Entity Framework 4 ctp5 一张表有同一个表的两个外键
我有两个表:
用户和新闻。表 News 具有列 id、name、createdby、modifiedby。表 user 有列 id 和 name。表News 通过外键createdby 和modifiedby 引用表User。我用 实体框架 4 CTP5。我尝试访问创建者或修改者属性,但出现错误:无效的列名称“UserId1”,无效的列名称“UserId2”。有人可以解释我做错了什么吗?
public class News
{
// Primitive properties
public int ID { get; set; }
public string Name { get; set; }
public System.Guid UserId { get; set; }
public System.Guid UserId1 { get; set; }
// Navigation properties
public virtual User User { get; set; }
public virtual User User1 { get; set; }
}
public class User
{
public int ID { get; set; }
public string Name { get; set; }
}
public partial class SomeEntities : DbContext
{
public SomeEntities() : base("name=SomeEntities"){ }
public DbSet<User> Users { get; set; }
public DbSet<News> News { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<News>().Property(i => i.UserId).HasColumnName("CreatedBy");
modelBuilder.Entity<News>().Property(i => i.UserId1).HasColumnName("ModifiedBy");
}
}
I Have two tables:
Users and News. Table News has columns id, name, createdby, modifiedby. Table user has columns id and name. Table News references table User by foreign keys createdby and modifiedby. I use
Entity Framework 4 ctp5. I tried to access createdby or modifiedby properties, but error appers: Invalid column name "UserId1", Invalid column name "UserId2". Could anybody explain what I am doing wrong?
public class News
{
// Primitive properties
public int ID { get; set; }
public string Name { get; set; }
public System.Guid UserId { get; set; }
public System.Guid UserId1 { get; set; }
// Navigation properties
public virtual User User { get; set; }
public virtual User User1 { get; set; }
}
public class User
{
public int ID { get; set; }
public string Name { get; set; }
}
public partial class SomeEntities : DbContext
{
public SomeEntities() : base("name=SomeEntities"){ }
public DbSet<User> Users { get; set; }
public DbSet<News> News { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<News>().Property(i => i.UserId).HasColumnName("CreatedBy");
modelBuilder.Entity<News>().Property(i => i.UserId1).HasColumnName("ModifiedBy");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下对象模型为您提供了所需的结果(我重命名属性只是为了使其更具可读性):
The following object model gives you the desired results (I renamed properties just to make it more readable):