I am using the Code First constructs to connect to an existing database (which, I know is not code first technically). I do this so that I can mock out Fake Data Repositories using LINQ to Entities instead of LINQ to SQL. In the CTP3 last year, this worked great - I could create a new Configuration for my table, and explicitly add the properties of the POCO that were mapped to the database.
In July 2011, Code First was included in EF4.1 so I thought I would upgrade. Unfortunately, EF is now too smart for my liking. For example, a) all properties of my POCOs are assumed to be mapped to database fields, so for example, a property Person.TempPassword{get;set;} causes the generated SQL to search for a TempPassword column in my Person table; b) if I inherit a class from my POCO (for example, Employee inherits from Person), EF starts generating Discriminator columns etc.
I know that I can resolve the former problem using the Ignore() or [NotMapped] attributes, however that is a big hassle and I'd prefer not to do it. The latter problem I cannot resolve.
All I need is a way to be able to turn off all these smarts, and just let me explicitly set properties as I used to do. Note that I have tried removing every single one of the constraints as well, for example modelBuilder.Conventions.Remove();
I have also overriden the call to OnModelCreating(), and NOT called base.OnModelCreating(), just included my own Configuration code, as below.
An example of my configuration is:
public class AddressBookConfiguration : EntityTypeConfiguration<AddressBook>{
public AddressBookConfiguration()
{
this.HasKey(x => x.AddressBookID)开发者_Python百科;
this.ToTable("AddressBook");
this.Property(x => x.AddressBookID);
this.Property(x => x.AddressBookName);
this.Property(x => x.WhenCreated);
this.Property(x => x.TotalItems);
}
}
Thank you.
The path I usually take when need to expand Entity Framework classes functionality is to generate the Data Model plainly from the database, and than use Extension Methods.
I know this is not sufficient, and you can not create Properties this way. However, I'm afraid this might be the best you can get.
精彩评论