ef-code-first

How to configure EF Tracing provider for EF code first

核能气质少年 提交于 2020-01-16 19:08:14
问题 i am trying to use EF Tracing utility with EF 5.0 (code first). but this works only object context which requires edmx file. http://code.msdn.microsoft.com/EFProviderWrappers anybody is having workaround for EF code first with DBContext? anand 回答1: From the Q&A section of the site, the author has this code for using Code First: Use the DbCommand constructor overload in your DbContext... var context = new NorthwindContext(CreateConnectionWrapper(@"name=NorthwindContext")); And the

Edit Method in MVC4 and Visual Studio 2012

点点圈 提交于 2020-01-16 08:52:08
问题 Could someone please help me with the question i posted late last night. I am still stuck with the edit method. Link below: Last night question In addition to what was posted, i have also tried: HttpPost Edit: [HttpPost] public ActionResult Edit(MensPlayer mensPlayer) { if (ModelState.IsValid) { //Save Player _dataSource.Entry(mensPlayer).State = EntityState.Modified; _dataSource.Save(); TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name); return RedirectToAction(

EF Code First: Duplicate foreign keys (one from name convention, one from navigation property)

佐手、 提交于 2020-01-16 05:31:47
问题 I'm trying to create an SQL database by using EF Code First. Assume I have the following code: public class Account { public int Id; public ICollection<User> Users; } public class User { public int Id; public int AccountId; } public class AccountContext : DbContext { public DbSet<Account> Accounts; public DbSet<User> Users; } (Note the lack of any Fluent API commands or Data Annotations; I want to do this by convention.) When the database is created, I get the following fields in the Users

EF “Model First”: Create entity in edmx and db-table from class?

孤街浪徒 提交于 2020-01-16 05:26:17
问题 This question is part of my following one I already asked roughly 2.5 years ago: Entity Framework (.NET) Round-Trip Modelling with Model First? I wanted to ask if in the meantime, the following is possible (and from which version of EF upwards): We are currently using EF 5.0 with the "Model First" approach (VS 2012). After first problems, we managed to configure the two T4 templates for generating the DbContext-derived class and the POCOs so that if we need to map a new (or existing) database

Custom Convention for table with Prefix Entity Framework

眉间皱痕 提交于 2020-01-16 02:08:08
问题 All my POCO classes have two letter prefix LK_. Entity framework conventions for primary & foreign keys would not work. Considering I have ~200 classes to decorate with Key or ForeignKey attribute its a cumbersome process & does not sound like a smart way of doing it. Could you please suggest custom convention? public class LK_Employee { public Guid EmployeeID {get; set;} public string Name {get; set;} } public class LK_Company { public Guid CompanyID {get; set;} public string Name {get; set;

Auditing a Table with EF Code first

强颜欢笑 提交于 2020-01-15 10:39:28
问题 I have a class say Message like following: public class Message { public int Id{get;set;} public string MessageText{get;set;} public int Sender{get;set;} public DateTime CreatedOn{get;set;} //EDIT:2 public virtual Message RepliedTo{get;set;} public virtual IList<Message> Replies{get;set;} public virtual IList<MessageStatusHistory> History{get;set;} //so on and so forth } Now I want to keep statuses of the Message object like what user marked it as read and when. I created a class

Database First to Code First EF - Migrations Key and Constraint Names not matching DB

烈酒焚心 提交于 2020-01-14 14:23:57
问题 I recently updated my project from Database First to a Code First Model, using this method: Link Everything seemed to be working until I wanted to update my FK and PKs on an existing table. This was a 1-0, 1-1 relationship. So the PK of Company table was the FK and PK of the DriverScorecardSetting table. So this is the entity that the Tool generated for the DriverScorecardSetting table. [Table("DriverScorecardSetting")] public partial class DriverScorecardSetting { [Key] [DatabaseGenerated

Database First to Code First EF - Migrations Key and Constraint Names not matching DB

柔情痞子 提交于 2020-01-14 14:23:06
问题 I recently updated my project from Database First to a Code First Model, using this method: Link Everything seemed to be working until I wanted to update my FK and PKs on an existing table. This was a 1-0, 1-1 relationship. So the PK of Company table was the FK and PK of the DriverScorecardSetting table. So this is the entity that the Tool generated for the DriverScorecardSetting table. [Table("DriverScorecardSetting")] public partial class DriverScorecardSetting { [Key] [DatabaseGenerated

Many to many relationship detecting changes

荒凉一梦 提交于 2020-01-14 09:23:31
问题 I am trying to understand why DbContext doesn't detect changesin many-to-many relationship. This is what I have set in model configuration: this.Configuration.ValidateOnSaveEnabled = false; this.Configuration.ProxyCreationEnabled = false; this.Configuration.LazyLoadingEnabled = false; This is the test code: var book = Context.Set<Book>().Where(b => b.Id == 1).Single(); var author = Context.Set<Author>().Where(a => a.Id == 2).Single(); book.Authors.Add(author); if I check for changes, it doesn

Many to many relationship detecting changes

做~自己de王妃 提交于 2020-01-14 09:23:29
问题 I am trying to understand why DbContext doesn't detect changesin many-to-many relationship. This is what I have set in model configuration: this.Configuration.ValidateOnSaveEnabled = false; this.Configuration.ProxyCreationEnabled = false; this.Configuration.LazyLoadingEnabled = false; This is the test code: var book = Context.Set<Book>().Where(b => b.Id == 1).Single(); var author = Context.Set<Author>().Where(a => a.Id == 2).Single(); book.Authors.Add(author); if I check for changes, it doesn