Entity Framework 4: Table for Insertion Inheritance Problem Type

I have a simple model with "BaseEntity" and a derivative object "Fund". When I try to insert a new fund:

HybridModelContainer container = new HybridModelContainer(); //Create new Fund Fund fund = new Fund(); fund.Id = Guid.NewGuid(); fund.DateCreated = DateTime.Now; fund.Name = "Fund 1"; fund.Number = 1; container.BaseEntities.AddObject(fund); container.SaveChanges(); 

I get the following error:

"Cannot insert NULL value in column" Id ", table" HybridData.dbo.BaseEntities ", column does not allow zeros. INSERT does not work.
Application completed.

It seems that the identifier assigned to the fund object is not inserted into the BaseEntity table. Why not?

I made this "first model". If I first create a database and create a model from it, everything will work well ... But first I need a model!

enter image description here

Also ... why is there no ObjectSet for " Funds " in the DataContext (ie container.Funds )? Thanks in advance for your help!

+7
source share
1 answer

I can only answer the second part of your question: "Also ... why is there no ObjectSet for" funds "in my DataContext (ie container.Funds)?"

It is normal that an ObjectContext has only an ObjectSet of a base class in your class hierarchy. In fact, there is no need for an ObjectSet for derived classes. To add and remove derived objects to / from an ObjectContext, you can simply use the AddObject and DeleteObject ObjectSet<BaseEntity> / BaseEntities , as you already did in your example.

For queries on derived objects, you can use the OfType method of an ObjectSet. It returns an ObjectQuery of a derived type, where you can create additional queries:

 ObjectQuery<Fund> query = container.BaseEntities.OfType<Fund>(); 

(The first part of your question sounds like a mapping error between the repository and the conceptual model. Perhaps you will have the best cif that you could show in the relevant parts of your edmx file.)

Edit: A possible solution to the first part of the question:

I created your example with Model-first in VS2010, and I could reproduce your problem. The problem seems to be related to the fact that the primary key in your model is not Int32 , but a Guid . When you add a new Entity to the model, the designer always offers Int32 as the primary key with StoreGeneratedPattern set to Identity .

If you now change the key type in the model designer from Int32 to Guid , but leave StoreGeneratedPattern without changing Identity , DDL creates a database on SQL Server with the Id set to type uniqueidentifier , but the identifier specification for this column is "No".

So, when EF sends the INSERT command to the database, it β€œthinks” from the model definition, the primary one will be autogenerated in the database and not send Guid to the database that you assigned in the code. But the database does not create the key, which results in a NULL value for the key. From here we get an error.

Decision. Set the Id BaseEntity StoreGeneratedPattern - None property in the model constructor. It worked for me. Of course, you are responsible for always assigning Guid to Id before adding a new object to the ObjectSet, but this is similar to what you want anyway.

+9
source

All Articles