实体框架中的单向一对一关系 [英] Unidirectional One-To-One relationship in Entity Framework

查看:99
本文介绍了实体框架中的单向一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的一对一关系示例引发异常

  public class User 
{
public int Id {get;组; }
public Address Address {get;组;
}

public class Address
{
public int Id {get;组; }
public User User {get;组;
}

异常说:


无法确定
类型ConsoleApplication1.Address和ConsoleApplication1.User之间的关联的主体结束。
必须使用流畅的API或数据注释关系来明确地配置此关联的主体结束。

如果我从地址中删除用户属性,但是我不想要,它可以工作。



我如何有这样的关系,没有例外?

$ b $虽然Eranga提供的答案是正确的,但是创建了一个

04/14 / associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspxrel =nofollow noreferrer>共享主键关联在用户和地址,由于该映射类型的限制,您可能不想使用它。



这是创建1:1关联的另一种方法,称为一对一的外键关联

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< ;地址>()
.HasRequired(a => a.User)
.WithOptional(u => u.Address)
.Map(m => m.MapKey 用户名));
}

EF代码首先将此识别为1:1关联,从而允许您用户和地址之间的双向关联。



现在您需要做的是在UserId列上定义唯一键约束,使您的关系在数据库端成为真正的一对一。一种方法是使用已经在自定义初始化程序类中覆盖的Seed方法:

  class DbInitializer:DropCreateDatabaseAlways< Context> ; 
{
protected override void Seed(Context context)
{
context.Database.ExecuteSqlCommand(ALTER TABLE Addresses ADD CONSTRAINT uc_User UNIQUE(UserId));
}
}



上面的代码将导致以下模式:




Below example of one-to-one relationship throws an exception

public class User
{
    public int Id { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public int Id { get; set; }
    public User User { get; set; }
}

Exception says:

Unable to determine the principal end of an association between the types 'ConsoleApplication1.Address' and 'ConsoleApplication1.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

it works if i remove User property from Address but i don't want.

How can i have such a relationship without an exception ?

解决方案

While the answer provided by Eranga is correct and creates a shared primary key association between User and Address, you might not want to use it due to the limitations this mapping type has.

Here is another way of creating a 1:1 association which is called one-to-one foreign key association:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Address>()
                .HasRequired(a => a.User)
                .WithOptional(u => u.Address)
                .Map(m => m.MapKey("UserId"));
}

EF Code First recognizes this as a 1:1 association hence allowing you to have a bidirectional association between User and Address.

Now all you need to do is to define a Unique Key constraint on the UserId column to make your relationship a true one to one on your database side. One way for doing so is using a Seed method that has been overridden in a custom initializer class:

class DbInitializer : DropCreateDatabaseAlways<Context>
{
    protected override void Seed(Context context)
    {
        context.Database.ExecuteSqlCommand("ALTER TABLE Addresses ADD CONSTRAINT uc_User UNIQUE(UserId)");
    }
}


The above code will result in the following schema:

这篇关于实体框架中的单向一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆