微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决EF 在同一个主键 ApplicationUser 表上的多个外键关系

我想通过代码优先的方法使用 EF 6 创建一对多关系。

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

我还希望与同一个 UpdatedById 表建立 ApplicationUser 关系,以便能够在 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);

解决方法

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

例如

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();
               

            }


        }
    }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。