如何使用Code-First注释父子关系 [英] how to annotate a parent-child relationship with Code-First

查看:82
本文介绍了如何使用Code-First注释父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架代码优先库的CTP 5时(如宣布
这里
)我正在尝试创建一个映射到的类一个非常简单的层次表。

When using the CTP 5 of Entity Framework code-first library (as announced here) I'm trying to create a class that maps to a very simple hierarchy table.

这是构建表的SQL:

CREATE TABLE [dbo].[People] 
( 
 
Id  uniqueidentifier not null primary key rowguidcol, 
 
Name  nvarchar(50) not null, 
 
Parent  uniqueidentifier null 
) 
ALTER TABLE
[dbo].[People] 
 ADD CONSTRAINT
[ParentOfPerson]  
 FOREIGN KEY
(Parent) 
 REFERENCES
People (Id) 

这是我希望自动映射回该表的代码:

Here's the code that I would hope to have automatically mapped back to that table:

class Person 
{ 
   
public Guid Id { get; set; } 
   
public String Name { get; set; } 
   
public virtual Person Parent { get; set; } 
   
public virtual ICollection<Person> Children { get; set; } 
} 
 
class FamilyContext : DbContext 
{ 
   
public DbSet<Person> People { get; set; } 
} 

我在app.config文件中设置了connectionstring如此:

I have the connectionstring setup in the app.config file as so:

<configuration> 
 
<connectionStrings> 
   
<add name="FamilyContext" connectionString="server=(local); database=CodeFirstTrial; trusted_connection=true" providerName="System.Data.SqlClient"/> 
 
</connectionStrings> 
</configuration> 

最后我想尝试使用这个类来添加父类和子实体,如下所示:

And finally I'm trying to use the class to add a parent and a child entity like this:

static void Main(string[] args) 
{ 
   
using (FamilyContext context = new FamilyContext()) 
   
{ 
       
var fred = new Person 
       
{ 
           
Id = Guid.NewGuid(), 
           
Name = "Fred" 
       
}; 
       
var pebbles = new Person 
       
{ 
           
Id = Guid.NewGuid(), 
           
Name = "Pebbles", 
           
Parent = fred 
       
}; 
        context
.People.Add(fred); 
       
var rowCount = context.SaveChanges(); 
       
Console.WriteLine("rows added: {0}", rowCount); 
       
var population = from p in context.People select new { p.Name }; 
       
foreach (var person in population) 
           
Console.WriteLine(person); 
   
} 
} 

这里显然缺少一些东西。我得到的例外是:

There is clearly something missing here. The exception that I get is:


无效的列名'PersonId'。

Invalid column name 'PersonId'.

我理解约定对配置的价值,我和我的团队对放弃edmx /设计师噩梦的前景感到非常兴奋 - 但似乎没有关于约定的明确文件。 (我们很幸运地获得了多个表名的
,用于单数类名)

I understand the value of convention over configuration, and my team and I are thrilled at the prospect of ditching the edmx / designer nightmare --- but there doesn't seem to be a clear document on what the convention is. (We just lucked into the notion of plural table names, for singular class names)

如何使这个非常简单的例子落实到位的一些指导将不胜感激。

Some guidance on how to make this very simple example fall into place would be appreciated.

推荐答案

...试试我几天前在这里建议的... ...
HTTP:/ /social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/30c4fa2a-ae2a-4494-908e-f6c1f7cbc32f/#bbfc7eb4-c035-4c34-bde5-f217bb541828

......几乎相同的是
你必须明确指定父关系 

...it's pretty much the same
you have to explicitly specify the parent relation 

modelBuilder.Entity< Person >()。HasOptional(n
=> n.Parent).WithMany()。IsIndependent();


......没有孩子

或者您可以尝试将孩子包括在'withmany'中(例如WithMany(p => p。 儿童

希望它有所帮助,

Gaga

modelBuilder.Entity<Person>().HasOptional(n => n.Parent).WithMany().IsIndependent();
...that's w/o children
or you can try including the children in the 'withmany' (e.g. WithMany(p=>p.Children)
hope it helps,
Gaga



这篇关于如何使用Code-First注释父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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