简体   繁体   English

EF代码优先-System.InvalidOperationException

[英]EF Code First - System.InvalidOperationException

I'm new to EF Code First, and when I try to implement the same facing the error as follow, 我是EF Code First的新手,当我尝试实现以下相同的错误时,

Actions - While running the application: 动作-在运行应用程序时:

1.Very first time after migrating the tables, fresh record is inserted successfully on 'GroupPrivileges'. 1.在第一次迁移表之后,新记录成功插入到“ GroupPrivileges”上。

2.While updating the same record of 'GroupPrivileges' it throws error. 2.更新“ GroupPrivileges”的相同记录时,将引发错误。

3.Similarly when I try to insert a new second record on 'GroupPrivileges' the same error pops out. 3.类似地,当我尝试在“ GroupPrivileges”上插入新的第二条记录时,会弹出相同的错误。

Error : 错误:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理

Additional information: An object of type 'System.Collections.Generic.List`1[[Dsms.Model.User.GroupModel, Dsms.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot be set or removed from the Value property of an EntityReference of type 'Dsms.Model.User.GroupModel'. 附加信息:不能设置类型为'System.Collections.Generic.List`1 [[Dsms.Model.User.GroupModel,Dsms.Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'的对象或从类型为'Dsms.Model.User.GroupModel'的EntityReference的Value属性中删除。

在此处输入图片说明

在此处输入图片说明

Please help me resolve this issue, I'm unable to take a step further :-( 请帮助我解决此问题,我无法采取进一步措施:-(

With this please find the below model and the DBConfig classes. 有了这个,请找到下面的模型和DBConfig类。

public abstract class BaseModel
{
    public int DrivingSchoolId { get; set; }
    public int UserId { get; set; }
    public string ExternalId { get; set; }
    [Display(Name = "Active")]
    public bool IsActive { get; set; }
    public int CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
    public int ModifiedBy { get; set; }
    public DateTime ModifiedOn { get; set; }

    public BaseModel()
    {
        IsActive = true;
        CreatedOn = DateTime.Now;
        ModifiedOn = DateTime.Now;
        ExternalId = Guid.NewGuid().ToString("N");    
    }
}

public class GroupModel : BaseModel
{
    [Key]
    public int GroupId { get; set; }

    [Display(Name = "Name")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please fill the 'Name'")]
    [MaxLength(100, ErrorMessage = "Maximum character lenght of 'Name' is 100.")]
    public string Name { get; set; }

    [Display(Name = "Code")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please fill the 'Code'")]
    [MaxLength(15, ErrorMessage = "Maximum character lenght of 'Code' is 15.")]
    public string Code { get; set; }

    [Display(Name = "Description")]
    [DataType(DataType.MultilineText)]
    [MaxLength(500, ErrorMessage = "Maximum character lenght of 'Description' is 500.")]
    public string Description { get; set; }

    //EF Navigation properties starts
    public virtual ICollection<DrivingSchoolModel> DrivingSchools { get; set; }
    public virtual UserGroupModel UserGroup { get; set; }
    public virtual GroupPrivilegeModel GroupPrivilege { get; set; }
}

public class RoleModel : BaseModel
{
    [Key]
    public int RoleId { get; set; }

    [Display(Name = "Name")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please fill the 'Name'")]
    [MaxLength(100, ErrorMessage = "Maximum character lenght of 'Name' is 100.")]
    public string Name { get; set; }

    [Display(Name = "Code")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please fill the 'Code'")]
    [MaxLength(15, ErrorMessage = "Maximum character lenght of 'Code' is 15.")]
    public string Code { get; set; }

    [Display(Name = "Description")]
    [DataType(DataType.MultilineText)]
    [MaxLength(500, ErrorMessage = "Maximum character lenght of 'Description' is 500.")]
    public string Description { get; set; }

    //EF Navigation properties starts
    public virtual GroupPrivilegeModel GroupPrivilege { get; set; }
    public virtual UserRoleModel UserRole { get; set; }        
}

public class GroupPrivilegeModel : BaseModel
{
    [Key]
    public int GroupPrivilegeId { get; set; }

    [Display(Name = "Group")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please select the 'Group'")]
    public int GroupId { get; set; }

    [Display(Name = "Role")]
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please select the 'Role'")]
    public int RoleId { get; set; }

    //EF Navigation properties starts
    public virtual ICollection<GroupModel> Groups { get; set; }
    public virtual ICollection<RoleModel> Roles { get; set; }
}

    public GroupConfiguration()
    {
        this.ToTable("Groups", DsmsContext.DEFAULT_DB_SCHEMA);

        this.HasKey(g => g.GroupId);

        this.HasRequired(g => g.DrivingSchools).WithMany().HasForeignKey(g => g.DrivingSchoolId);

        this.Property(g => g.ExternalId).HasColumnName("ExternalGroupId").HasMaxLength(50).IsRequired();
        this.Property(g => g.Name).HasMaxLength(100).IsRequired();
        this.Property(g => g.Code).HasMaxLength(15).IsRequired();
        this.Property(g => g.Description).HasMaxLength(500).IsOptional();
        this.Property(g => g.DrivingSchoolId).IsRequired();
        this.Property(g => g.IsActive).IsRequired();
        this.Property(g => g.CreatedBy).IsRequired();
        this.Property(g => g.CreatedOn).IsRequired();
        this.Property(g => g.ModifiedBy).IsOptional();
        this.Property(g => g.ModifiedOn).IsOptional();

        this.Ignore(g => g.UserId);
        this.Ignore(g => g.UserGroup);
        this.Ignore(g => g.GroupPrivilege);
    }

    public RoleConfiguration()
    {
        this.ToTable("Roles", DsmsContext.DEFAULT_DB_SCHEMA);

        this.HasKey(r => r.RoleId);

        this.Property(r => r.ExternalId).HasColumnName("ExternalRoleId").HasMaxLength(50).IsRequired();
        this.Property(r => r.Name).HasMaxLength(100).IsRequired();
        this.Property(r => r.Code).HasMaxLength(15).IsRequired();
        this.Property(r => r.Description).HasMaxLength(500).IsOptional();
        this.Property(r => r.IsActive).IsRequired();
        this.Property(r => r.CreatedBy).IsRequired();
        this.Property(r => r.CreatedOn).IsRequired();
        this.Property(r => r.ModifiedBy).IsOptional();
        this.Property(r => r.ModifiedOn).IsOptional();

        this.Ignore(r => r.DrivingSchoolId);
        this.Ignore(r => r.UserId);
        this.Ignore(r => r.GroupPrivilege);
        this.Ignore(r => r.UserRole);
    }

    public GroupPrivilegeConfiguration()
    {
        this.ToTable("GroupPrivileges", DsmsContext.DEFAULT_DB_SCHEMA);

        this.HasKey(gp => gp.GroupPrivilegeId);

        this.HasRequired(gp => gp.Groups).WithMany().HasForeignKey(gp => gp.GroupId);
        this.HasRequired(gp => gp.Roles).WithMany().HasForeignKey(gp => gp.RoleId);

        this.Property(gp => gp.IsActive).IsRequired();
        this.Property(gp => gp.CreatedBy).IsRequired();
        this.Property(gp => gp.CreatedOn).IsRequired();
        this.Property(gp => gp.ModifiedBy).IsOptional();
        this.Property(gp => gp.ModifiedOn).IsOptional();

        this.Ignore(gp => gp.DrivingSchoolId);
        this.Ignore(gp => gp.UserId);
        this.Ignore(gp => gp.ExternalId);
    }

In your update method it will be better if you get rid of the existingRecord variable and just go with: 在您的更新方法中,如果您摆脱了existingRecord变量,而只需执行以下操作,那就更好了:

_contex.Entry(model).State = System.Data.Entity.EntityState.Modified;
_context.SaveChages();

The same with the Insert Method : 与Insert方法相同:

 _context.GroupePrivilages.Add(model);
 _context.SaveChages();

Also: 也:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM