Entity Framework 4 ctp5 一张表有同一个表的两个外键

发布于 2024-10-16 08:59:35 字数 1143 浏览 0 评论 0原文

我有两个表:

用户和新闻。表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-10-23 08:59:35

以下对象模型为您提供了所需的结果(我重命名属性只是为了使其更具可读性):

public class News
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Guid CreatedBy { get; set; }
    public Guid ModifiedBy { get; set; }
    public virtual User CreatedByUser { get; set; }
    public virtual User ModifiedByUser { get; set; }
}

public class User
{
    public Guid ID { get; set; }
    public string Name { get; set; }
}

public class Ctp5Context : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<News> News { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<News>()
                    .HasRequired(n => n.CreatedByUser)
                    .WithMany()
                    .HasForeignKey(n => n.CreatedBy);

        modelBuilder.Entity<News>()
                    .HasRequired(n => n.ModifiedByUser)
                    .WithMany()
                    .HasForeignKey(n => n.ModifiedBy)
                    .WillCascadeOnDelete(false);                        
    }
}

The following object model gives you the desired results (I renamed properties just to make it more readable):

public class News
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Guid CreatedBy { get; set; }
    public Guid ModifiedBy { get; set; }
    public virtual User CreatedByUser { get; set; }
    public virtual User ModifiedByUser { get; set; }
}

public class User
{
    public Guid ID { get; set; }
    public string Name { get; set; }
}

public class Ctp5Context : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<News> News { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<News>()
                    .HasRequired(n => n.CreatedByUser)
                    .WithMany()
                    .HasForeignKey(n => n.CreatedBy);

        modelBuilder.Entity<News>()
                    .HasRequired(n => n.ModifiedByUser)
                    .WithMany()
                    .HasForeignKey(n => n.ModifiedBy)
                    .WillCascadeOnDelete(false);                        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文