实体框架使用复合键插入记录而不插入第二个字段

时间:2017-08-31 18:36:51

标签: c# oracle entity-framework

我遇到了使用Entity Framework 6插入子对象的问题。

我有两个班级:

public class RoleInstance
{
    [Key, Column(Order = 1)]
    public long RoleId { get; set; }
    [Key, Column(Order = 2)]
    public string EnvirCode { get; set; }

    public int PathId { get; set; }

    public char Published { get; set; }

    [ForeignKey("RoleId")]
    public virtual Role Role { get; set; }
    [ForeignKey("PathId")]
    public virtual RoutingPath RoutingPath { get; set; }
    [ForeignKey("EnvirCode")]
    public virtual Environment Environment { get; set; }

    public ICollection<ActiveDirectoryGroup> ActiveDirectoryGroups { get; set; }
}

public class ActiveDirectoryGroup
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Column(Order = 1), Key]
    public long RoleId { get; set; }
    [Column(Order = 2), Key]
    public string EnvirCode { get; set; }
    public string GroupName { get; set; }
    public string GroupGuid { get; set; }
    public char AuditGroup { get; set; }

    [ForeignKey("RoleId,EnvirCode")]
    public virtual RoleInstance RoleInstance { get; set; }
}

我正在尝试使用以下代码插入记录:

RoleInstance ri = new RoleInstance { RoleId = 1, 
                                     EnvirCode = "ENVIR", 
                                     PathId = 5,
                                     Published = 'Y' 
                                     ActiveDirectoryGroups = new List<ActiveDirectoryGroups>()
}; 

ri.ActiveDirectoryGroups.Add(new ActiveDirectoryGroup() { 
 GroupName = "GROUP NAME", 
 AuditGroup = 'Y'
}; 

_db.Context.Add(ri); 

然后当我保存它时,它将记录插入RoleInstance,实体框架没有在EnvirCode中设置ActivityDirectoryGroup

这就是我modelBuilder的样子:

modelBuilder.Entity<ActiveDirectoryGroup>()
            .HasRequired(e => e.RoleInstance)
            .WithMany(e => e.ActiveDirectoryGroups)
            .HasForeignKey(e => new {e.RoleId, e.EnvirCode}); 

1 个答案:

答案 0 :(得分:0)

如果两者的键都是(RoleId,EnvirCode),则RoleInstance不能有多个ActiveDirectoryGroup。将ActiveDirecoryGroups的密钥切换为Id,或将(RoleId,EnvirCode,Id)切换为一对多关系。而且您不需要在此处混合使用Fluent和基于属性的映射。

这是一个工作模式:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace Ef6Test
{

    public class Role
    {
        public long RoleId { get; set; }
    }
    public class RoutingPath
    {
        public int RoutingPathId { get; set; }
    }
    public class Environment
    {
        public string EnvironmentId { get; set; }
    }
    public class RoleInstance
    {
        [Key, Column(Order = 1)]
        public long RoleId { get; set; }
        [Key, Column(Order = 2)]
        public string EnvirCode { get; set; }

        public int PathId { get; set; }

        public char Published { get; set; }

        [ForeignKey("RoleId")]
        public virtual Role Role { get; set; }
        [ForeignKey("PathId")]
        public virtual RoutingPath RoutingPath { get; set; }
        [ForeignKey("EnvirCode")]
        public virtual Environment Environment { get; set; }

        public ICollection<ActiveDirectoryGroup> ActiveDirectoryGroups { get; } = new HashSet<ActiveDirectoryGroup>();
    }

    public class ActiveDirectoryGroup
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
        public int Id { get; set; }

        public long RoleId { get; set; }

        public string EnvirCode { get; set; }
        public string GroupName { get; set; }
        public string GroupGuid { get; set; }
        public char AuditGroup { get; set; }

        [ForeignKey("RoleId,EnvirCode")]
        public virtual RoleInstance RoleInstance { get; set; }
    }

    class Db : DbContext
    {

        public DbSet<ActiveDirectoryGroup> ActiveDirectoryGroups { get; set; }
        public DbSet<RoleInstance> RoleInstances { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<Db>());

            using (var db = new Db())
            {
                db.Database.Initialize(false);

                db.Database.Log = msg => Console.WriteLine(msg);

                RoleInstance ri = new RoleInstance
                {
                    Role = new Role() { RoleId = 1},
                    Environment = new Environment() {  EnvironmentId = "ENVIR" },
                    RoutingPath = new RoutingPath() {  RoutingPathId = 5 },
                    Published = 'Y'
                };

                ri.ActiveDirectoryGroups.Add(new ActiveDirectoryGroup()
                {
                    GroupName = "GROUP NAME",
                    AuditGroup = 'Y'
                });

                db.RoleInstances.Add(ri);
                db.SaveChanges();


            }

            using (var db = new Db())
            {
                foreach (var g in db.ActiveDirectoryGroups)
                {
                    Console.WriteLine($"RoleId: {g.RoleId}, EnvirCode: { g.EnvirCode}");
                }
            }

            Console.ReadKey();
        }
    }
}