【发布时间】:2011-01-17 16:35:12
【问题描述】:
我正在尝试在两个表之间创建一对一的关系,但结果是我是一对多的。这段代码有什么问题?
namespace EFCF_Demo.Models
{
public class Post
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string MiniContent { get; set; }
public string Author { get; set; }
public DateTime PublishDate { get; set; }
public int Rating { get; set; }
public virtual Content MainContent { get; set; }
}
public class Content
{
public int ID { get; set; }
public virtual Post Post { get; set; }
public string FullContent { get; set; }
}
public class PostEntities : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Content> Contents { get; set; }
}
}
【问题讨论】:
-
我在 Content 类中没有看到 KeyAttribute。
-
密钥由 EFCF 自动检测,它是 - ID。我使用 fluent API 解决了这个问题,但 Steel 在保存内容方面存在一些问题。
-
如果我没记错的话,当属性命名为Id,或者ClassNameId时,假定为主键
标签: c# ef-code-first poco