实体框架代码第一个一对一关系 [英] Entity Framework Code First One to One Relationship

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

问题描述

我有以下两种模型

public class Account
{
     public int Id { get; set; }
     public string Name { get; set; }
     public int CurrentPeriodId { get; set; }

     [ForeignKey("CurrentPeriodId")]
     public virtual Period CurrentPeriod { get; set; }

     public virtual ICollection<Period> Periods { get; set; }
}

public class Period
{
     public int Id { get; set; }
     public int AccountId { get; set; }
     public DateTime StartDate { get; set; }
     public DateTime EndDate { get; set; }

     public virtual Account Account { get; set; }
}

我正在尝试运行以下查询:

And I am trying to run the following query:

from p in context.Periods
where p.AccountId == accountId &&
      p.Id == p.Account.CurrentPeriodId
select p.StartDate

并且我收到一条SQL异常,说无效的列名Account_AccountId。

And I get a sql exception saying "Invalid column name Account_AccountId".

我知道我可以从帐户一侧进行处理,并执行类似的操作

I know I could approach this from the Account side and do something like

from a in context.Accounts
where a.Id == id
select a.CurrentPeriod.StartDate

但我想知道如何设置关系以使其他查询正常工作。我缺少什么?

But I would like to know how to setup the relationship to get the other query to work. What am I missing?

推荐答案

我认为您的实现是错误的。 AFAIK,您无法在EF中使用此方法定义一对一关系。

查看生成的数据库,您有一个 Account_Id 列在 Periods 表中定义。您必须定义以下内容:

I think your implementation is wrong. AFAIK, You cannot define one-to-one relationship using this approach in EF.
Take a look at your generated database, you have an Account_Id column defined in your Periods table. Here's what you have to define:

public class Account
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int CurrentPeriodId { get; set; }
    public virtual Period CurrentPeriod { get; set; }
    public virtual ICollection<Period> Periods { get; set; }
}

public class Period
{
    public int Id { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

然后,上下文和初始值设定项:

Then, context and initializer:

public class TestContext : DbContext
{
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Period> Periods { get; set; }

static TestContext()
{ 
    Database.SetInitializer(new DbInitializer());
}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Account>().HasRequired(a => a.CurrentPeriod).WithMany().HasForeignKey(a => a.CurrentPeriodId);
    }
}

class DbInitializer : DropCreateDatabaseAlways<TestContext>
{
    protected override void Seed(TestContext context)
    {
        context.Database.ExecuteSqlCommand("ALTER TABLE Accounts ADD CONSTRAINT uc_Period UNIQUE(CurrentPeriodId)");
    }
}

有关一对一关系的更多信息,阅读Manavi先生的博客系列,网址为此地址

更新

根据您的评论,如果要引用期间中的帐户,则可以放置<$ c帐户主键上的$ c> ForeignKey 属性。

For more information about One-To-One relationship, read Mr. Manavi's blog series at this address.
Update:
Based on your comment, if you want to have a reference to Account from Period, You can put a ForeignKey attribute on your account's primary key.

一个很好的示例位于此地址(标题为将一对一映射为零或一个关系的部分)

A good sample is located at this address (the part with title "Map a One to Zero or One Relationship")

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

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