简体   繁体   中英

How do I add a Primary Key identity column to Entity Framework Code First generated Model

I add a web service (ASMX) to my VS2010 project via "Add Service Reference". This generates proxy classes in the Reference.cs file. Some of these classes are the return values for the methods that I call on the web service. I'm trying to persist these classes to my SQL Server database using Entity Framework 4.1 "Code First".

The problem that I run into is that these classes have no "key" defined. So that when I try to save the returned objects using EF, I get error messages like "EntityType 'MyReturnObject' has no key defined. Define the key for this EntityType" and a corresponding error message for the EntitySet.

What I'd like to do is add an identity column to the generated table for each class. I've done some research on the web and the place to do it would seem to be in the "OnModelCreating" event on my DbContext based class for my data model.

ie something like

protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
    modelBuilder.Entity<TheReturnObject>().AddProperty("MyNEWIdentityColumnName")...blah
}

where ... blah is whatever additional fluent config info is needed to tell EF to make this an identity column in the SQL Server database.

Intellisense tells me that there is no AddProperty method for Entity so just treat that as my way of indicating what I need, I'm aware it doesn't exist.

Then I also want any of the immediate child objects in the object graph to use that as their foreign key reference when saving. These child objects also have children so they need their own identity columns too, and so on.

If these were my own POCOs, I could just add the relevant properties and attributes to the classes in the source, but since they are proxies, I don't see doing that as an option.

Maybe I'm not seeing the forest for the trees, here. I'd greatly appreciate any advice in this matter.

Every class generated by Add service reference tool should be partial. So create your own partial part for each entity and add public Id property of type int . EF should automatically recognize this property as a key due to naming convention. This solution will allow you inserting new entities but it will not work for updates or deletes because in such case you need to know value of Id property.

EF is only able to work with properties defined in mapped classes. You cannot add additional columns through mappings (except discriminator in TPH inheritance).

Code first is an option for the Entity Framework to take some POCO classes that you written and generate a database structure for you.

You can annotate the model with DataAnnotations, for example to specify the Primary Key. You could also use the OnModelCreating and define the mapping trough a FluentAPI.

What you could do is build a POCO version of the return values from the WCF service and then map the WCF proxy to your POCO and add that to the database.

It is not possible to dynamically add a property to a POCO trough the mapping definitions. This would mean that some change would be made to the class definition of your POCO and that's not what mapping is for.

You could also look into writing your WCF proxy by hand instead of using the generated code. If you write them by hand you have free control over how they look and you could add the Id column to your handmade 'proxy-poco'.

Have a look at this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM