Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF同一主键ApplicationUser表上的多个外键关系_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# EF同一主键ApplicationUser表上的多个外键关系

C# EF同一主键ApplicationUser表上的多个外键关系,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我想使用EF6创建一对多关系,使用代码优先的方法 让我们举一个简单而经典的例子。我有两个实体Invoice和UserApplication,它们具有一对多关系: 我还希望与相同的ApplicationUser表建立UpdatedById关系,以便能够在UI中显示添加记录和修改记录的人的姓名 public partial class ApplicationUser : IdentityUser { public string FirstName { get; set; };

我想使用EF6创建一对多关系,使用代码优先的方法

让我们举一个简单而经典的例子。我有两个实体
Invoice
UserApplication
,它们具有一对多关系:

我还希望与相同的
ApplicationUser
表建立
UpdatedById
关系,以便能够在UI中显示添加记录和修改记录的人的姓名

 public partial class ApplicationUser : IdentityUser
    {
     public string FirstName { get; set; };
     public string  LastName { get; set; };
    }
     public virtual List<Invoice> Invoices { get; set; }
    
    
    
    public class Invoice
    {
     public int Id { get; set; }
     public string Details { get; set; }
     public string CreatedById { get; set; }
     public string UpdatedById { get; set; }
    }
     public virtual ApplicationUser CreatedBy { get; set; }
    
     builder.Entity<Invoice>()
     .HasOne(f => f.CreatedBy)
     .WithMany(mu => mu.Invoices)
     .HasForeignKey(f => f.CreatedById);
公共部分类应用程序用户:IdentityUser
{
公共字符串名{get;set;};
公共字符串LastName{get;set;};
}
公共虚拟列表{get;set;}
公共类发票
{
公共int Id{get;set;}
公共字符串详细信息{get;set;}
公共字符串CreatedById{get;set;}
公共字符串UpdatedById{get;set;}
}
公共虚拟应用程序用户由{get;set;}创建
builder.Entity()
.HasOne(f=>f.CreatedBy)
.WithMany(mu=>mu.发票)
.HasForeignKey(f=>f.CreatedById);

如果希望应用程序用户的导航属性用于这些关系,则需要创建和配置单独的属性

乙二醇

使用Microsoft.EntityFrameworkCore;
使用System.Linq;
使用制度;
使用System.Collections.Generic;
命名空间EfCore6Test
{
公共部分类ApplicationUser/:IdentityUser
{
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共虚拟ICollection InvoicesCreated{get;}=new HashSet();
公共虚拟ICollection InvoicesLastUpdated{get;}=new HashSet();
}
公共类发票
{
公共int Id{get;set;}
公共字符串详细信息{get;set;}
public int CreatedById{get;set;}
public int UpdatedById{get;set;}
公共虚拟应用程序用户由{get;set;}创建
公共虚拟应用程序用户LastUpdatedBy{get;set;}
}
公共类Db:DbContext
{
公共数据库集{get;set;}
公共数据库集用户{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(f=>f.CreatedBy)
.WithMany(mu=>mu.InvoicesCreated)
.HasForeignKey(f=>f.CreatedById)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity()
.HasOne(f=>f.lastUpdatedby)
.WithMany(mu=>mu.invoiceslastated)
.HasForeignKey(f=>f.UpdatedById)
.OnDelete(DeleteBehavior.Restrict);
基于模型创建(modelBuilder);
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseSqlServer(“服务器=localhost;数据库=efCore6Test;集成安全=true;信任服务器证书=true”,o=>o.UserRelationalNulls(true))
.LogTo(Console.WriteLine,Microsoft.Extensions.Logging.LogLevel.Information);
基本配置(选项生成器);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
{
使用var db=new db();
db.Database.EnsureDeleted();
db.Database.recreated();
}
}
}
}

或者干脆省略应用程序用户上的导航属性。

我仍然无法按预期实现此功能,如果您不介意的话,请您对整个场景做一个简短的示例。谢谢。见最新答案。你忘了问问题。
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System;
using System.Collections.Generic;

namespace EfCore6Test
{

    public partial class ApplicationUser //: IdentityUser
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual ICollection<Invoice> InvoicesCreated { get; } = new HashSet<Invoice>();
        public virtual ICollection<Invoice> InvoicesLastUpdated { get; } = new HashSet<Invoice>();
    }
    
    public class Invoice
    {
        public int Id { get; set; }
        public string Details { get; set; }
        public int CreatedById { get; set; }
        public int UpdatedById { get; set; }
        public virtual ApplicationUser CreatedBy { get; set; }
        public virtual ApplicationUser LastUpdatdBy { get; set; }
    }
    
    public class Db: DbContext
    {
        public DbSet<Invoice> Invoices{ get; set; }
        public DbSet<ApplicationUser> Users{ get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Invoice>()
                        .HasOne(f => f.CreatedBy)
                        .WithMany(mu => mu.InvoicesCreated)
                        .HasForeignKey(f => f.CreatedById)
                        .OnDelete(DeleteBehavior.Restrict); 

            modelBuilder.Entity<Invoice>()
                        .HasOne(f => f.LastUpdatdBy)
                        .WithMany(mu => mu.InvoicesLastUpdated)
                        .HasForeignKey(f => f.UpdatedById)
                        .OnDelete(DeleteBehavior.Restrict);

            base.OnModelCreating(modelBuilder);
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=localhost;database=efCore6Test;Integrated Security=true;TrustServerCertificate=true", o => o.UseRelationalNulls(true))
                .LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
            base.OnConfiguring(optionsBuilder);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            {
                
                using var db = new Db();
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
               

            }


        }
    }
}