开发者

Entity Framework 4 One-Way Nav / Relation. What am I doing wrong?

开发者 https://www.devze.com 2023-03-30 04:21 出处:网络
I\'m trying to make a basic Address class, that many different other classes can have (a Company can have a billing address and a shipping address, each employee has a home address, etc...).I am doing

I'm trying to make a basic Address class, that many different other classes can have (a Company can have a billing address and a shipping address, each employee has a home address, etc...). I am doing this model-first in EF4, and I thought I could do a 1-way-navigation, but this does not work. Here is what I have:

The edmx model. Note that I created a 1-1 relation, then deleted the navigation property on Address so that just Companies and Employees reference an Address but the address does not reference its owner.

Entity Framework 4 One-Way Nav / Relation. What am I doing wrong?

The generated SQL looks correct, in that it creates all 3 tables, then enforces the Company -> Address and the Employee -> Address relationships, and does not enforce an Address -> Company or Address -> Employee relationship.

-- Creating table 'Addresses'
CREATE TABLE [Addresses] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Street] nvarchar(4000)  NOT NULL开发者_运维问答,
    [City] nvarchar(4000)  NOT NULL,
    [State] nvarchar(4000)  NOT NULL,
    [Zip] nvarchar(4000)  NOT NULL
);
GO

-- Creating table 'Companies'
CREATE TABLE [Companies] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(4000)  NOT NULL,
    [Address_Id] int  NOT NULL
);
GO

-- Creating table 'Employees'
CREATE TABLE [Employees] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(4000)  NOT NULL,
    [Address_Id] int  NOT NULL
);
GO

-- Creating foreign key on [Address_Id] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [FK_CompanyAddress]
    FOREIGN KEY ([Address_Id])
    REFERENCES [Addresses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating foreign key on [Address_Id] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_EmployeeAddress]
    FOREIGN KEY ([Address_Id])
    REFERENCES [Addresses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

Now I want to insert a Company and its Address...

    [TestMethod]
    public void TestMethod1()
    {
        var context = new Model1Container();
        context.AddToCompanies(new Company
        {
            Name = "TestCompany",
            Address = new Address
            {
                Street = "TestAddress"
            }
        });
        context.SaveChanges();
    }

This test fails on SaveChanges() with the error:

System.Data.UpdateException: Entities in 'Model1Container.Addresses' participate in the 'EmployeeAddress' relationship. 0 related 'Employee' were found. 1 'Employee' is expected.

So it looks like EF is trying to enforce the non-navigable Address -> Employee relationship on the Company's Address...

How can I make this work? It seems like this kind of relationship would be fairly common practice... Thanks for any help!


You can't make it work. The enforcement is not in the database but in your model. You told EF that Address cannot exist without both Company and Employee (those one-to-one relations) - if fails before any SQL is generated. Moreover if you check the generated SQL those relations are not one-to-one but one-to-many because EF doesn't support this kind of one-to-one relation (it supports it only if dependent entity's PK is FK to principal entity).

0

精彩评论

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