开发者

Entity Framework with POCO Template and FKs in Model - Null Ref Exception

开发者 https://www.devze.com 2023-01-31 04:41 出处:网络
I\'m using Entity Fram开发者_如何学Goework 4 with the POCO code generation template from Microsoft downloaded from the Visual Studio gallery.I am also leaving the default option to \"Include foreign k

I'm using Entity Fram开发者_如何学Goework 4 with the POCO code generation template from Microsoft downloaded from the Visual Studio gallery. I am also leaving the default option to "Include foreign keys in model" selected when generating the EF model from the database.

I've been able to reproduce this problem with a very simple model, only two tables/classes in an optional one-to-many relationship. In this case, I'm using an Address and a Person. A Person can have one or zero addresses, and an Address can have zero to many People.

The tables look like this:

CREATE TABLE [dbo].[Person](
    [PersonID] [uniqueidentifier] NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
    [AddressID] [uniqueidentifier] NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([PersonID] ASC )

 CREATE TABLE [dbo].[Address](
    [AddressID] [uniqueidentifier] NOT NULL,
    [Street1] [nvarchar](50) NOT NULL,
    [Street2] [nvarchar](50) NULL,
    [City] [nvarchar](50) NOT NULL,
    [State] [char](2) NOT NULL,
    [Country] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED ([AddressID] ASC)

ALTER TABLE [dbo].[Person]  WITH CHECK ADD  CONSTRAINT [FK_Person_Address] 
FOREIGN KEY([AddressID])
REFERENCES [dbo].[Address] ([AddressID])

When I try to create an Address object and add it to an existing Person object pulled from the database, I get a null reference exception:

TestPOCOEntities ctx = new TestPOCOEntities();

var person = ctx.People.FirstOrDefault(p => p.PersonID == new Guid("58AD37B4-1EBE-4649-940C-A141732C9901"));

var addr = new Address {AddressID = Guid.NewGuid(), Street1 = "123 Main St"};
person.Address = addr; // This line throws the exception

ctx.SaveChanges();

Digging into the call stack, the exception isn't being thrown from my code, or even the template-generated code, but inside the runtime dynamic proxy for the Person class in the AddressID setter. (Specifically, the System.Data.Objects.EntityEntry.FixupEntityReferenceByForeignKey(EntityReference reference) method.)

This exception does not occur if I use the default EF code generation instead of the POCO template. It also does not occur if I use the POCO template but uncheck the "Include foreign keys in model" checkbox when generating the model from the database.

I can get the error to go away if I add the following:

var addr = new Address {AddressID = Guid.NewGuid(), Street1 = "123 Main St"};
ctx.Addresses.AddObject(addr); // Adding this line...
person.Address = addr; // Means no more exception here!

I don't see why the combination of using the POCO template and including foreign keys in the model should require this kind of code change when interacting with my persistent objects. Is this a known bug? Am I missing something? Is this by design for some reason?


this is a potential bug of EF, usually then refresh a created entity (resave changes with new data) solution:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/832af255-d1c2-41d5-9e95-9cdf3b15bb57

the cheat is add the entity to the collection context first

second assign to the father entity

third, savechanges normally!

enjoy

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号