English 中文(简体)
利用伙伴关系。 净额和实体框架:在1:1:1关系中增加儿童目标
原标题:Using ASP.Net and Entity Framework: adding a child object in a 1:1 relationship

这可能是我的 la,但我无法找到一个容易的例子。

举我喜欢的电视表演博览仪,我的例子如下:

Table Throne (ThroneID)
Table King (ThroneID)

采用实体框架4,我有两张表格,彼此关系1:1。 在法典中,我要将一位国王与一位国王联系起来。 国王可以有0或1位国王。 如果是1:Many,我有创建协会的Add()。 我用的是1:1。

(现在我认识到,我的例子不是最好的......但是,在这个例子中,国王公投会与王牌一样执行1:1)

GameOfThronesContext context = new GameOfThronesContext();
Throne t = new Throne();
King k = new King();
t.Kings.Add(k);  // doesn t work because "Add" isn t available
context.Thrones.AddObject(t);
最佳回答

the solution below if for EF Code First 4.1, it shows how to design the two classes in order to have a relationshiop one-to-zero/one, the result will be:

  • Throne{Id(PK), Name..}
  • King {Id(PK,FK), Name..}



public class Throne { public int Id { get; set; } public string Name { get; set; } public virtual King King { get; set; } }

public class King
{
    public int Id { get; set; }
    public virtual Throne Throne { get; set; }
    public string Name { get; set; }
}

The relationship is then defined in the OnModelCreating of the context or in a configuration class:


public class MyContext : DbContext
{
    public DbSet< Throne> Thrones { get; set; }
    public DbSet< King> Kings { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
//We define the key for the King table modelBuilder.Entity< King>().HasRequired(x => x.Throne); } }

You can then:

 var throne = new Throne(){Name = "First Throne"};
 var king = new King() { Name = "First King" };
 throne.King = king;
 context.Thrones.Add(throne);
 context.SaveChanges();
问题回答

Updated to reflect OP s comments:

var t = new Throne {
    Id = Guid.NewGuid(),
    //...
    King = new King {
        Id = Guid.NewGuid()
        //...
    }
};

context.Thrones.Add(t);
context.SaveChanges();

EF4.1足以克服制约因素。

因此,我回答了我自己的问题:MS安排了这一问题,以便你在这种情况下必须把父母分配给孩子。 《数据实体框架》产生的法典并没有造成出于某种原因将儿童分配给父母的能力。 我会发现这一点。 以下著作:

GameOfThronesContext context = new GameOfThronesContext();
Throne t = new Throne();
King k = new King();
//t.Kings.Add(k);  // doesn t work because "Add" isn t available
k.Throne = t;  // this is made available by the framework
context.Thrones.AddObject(t);

尽管如此,Gorge2giga表明如何加强这一模式,以便你能够把一名儿童添加到父母身上(我认为),以便你能够使用<代码>。 King = k;,这是我首先想采取的一种更好的解决办法。 Kudos。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签