C# 无法跟踪.Net core中类型的实例?
我在其中一个表中得到以下错误。该表没有主键。如何处理? 我正在尝试向表中添加新行 错误C# 无法跟踪.Net core中类型的实例?,c#,entity-framework,.net-core,entity-framework-core,C#,Entity Framework,.net Core,Entity Framework Core,我在其中一个表中得到以下错误。该表没有主键。如何处理? 我正在尝试向表中添加新行 错误 Unable to track an instance of type 'CommonDataZipInfo' because it does not have a primary key. Only entity types with primary keys may be tracked.' DataContext.cs modelBuilder.Entity<CommonDataZipInfo
Unable to track an instance of type 'CommonDataZipInfo' because it does not have a primary key.
Only entity types with primary keys may be tracked.'
DataContext.cs
modelBuilder.Entity<CommonDataZipInfo>(entity =>
{
entity.HasNoKey();
entity.ToTable("COMMON_DATA_ZIP_INFO");
entity.Property(e => e.AddDate).HasColumnType("datetime");
entity.Property(e => e.ManuscriptNum)
.HasColumnName("manuscript_num")
.HasMaxLength(32)
.IsUnicode(false);
entity.Property(e => e.ZipfileName)
.HasMaxLength(32)
.IsUnicode(false);
});
EF实体必须需要一个主键才能操作,并且该键可以存在于表中,也可以不存在于表中
根据我的经验,这是一个解决方法,我试图使用很少的列找到一个唯一的键,并在模型中定义它。也许您可以使用。EF实体必须需要一个主键才能操作,并且该键可以存在于表中,也可以不存在于表中
根据我的经验,这是一个解决方法,我试图使用很少的列找到一个唯一的键,并在模型中定义它。也许你可以用。我这里也有同样的问题。这是我的密码: [……] [……] [……] 并删除上下文中的HasNoKey: [……] [……]
这可能会解决它。我这里也有同样的问题。这是我的密码: [……] [……] [……] 并删除上下文中的HasNoKey: [……] [……]
这可能会解决问题。尝试阅读此内容以不跟踪地运行实体框架:尝试阅读此内容以不跟踪地运行实体框架:
var CommonDataZipInfo = new CommonDataZipInfo()
{
ManuscriptNum = ManuscriptNum,
ZipfileName = Path.GetFileName(fileName),
AddDate = DateTime.Now
};
context.CommonDataZipInfo.Add(CommonDataZipInfo);
context.SaveChanges();
public partial class Radusergroup
{
public string Username { get; set; }
public string Groupname { get; set; }
public int Priority { get; set; }
}
modelBuilder.Entity<Radusergroup>(entity =>
{
entity.HasNoKey();
entity.ToTable("radusergroup");
entity.HasIndex(e => e.Username)
.HasName("username");
entity.Property(e => e.Groupname)
.IsRequired()
.HasColumnName("groupname")
.HasMaxLength(64)
.HasDefaultValueSql("''''''");
entity.Property(e => e.Priority)
.HasColumnName("priority")
.HasColumnType("int(11)")
.HasDefaultValueSql("'1'");
entity.Property(e => e.Username)
.IsRequired()
.HasColumnName("username")
.HasMaxLength(64)
.HasDefaultValueSql("''''''");
});
public partial class Radusergroup
{
[Key]
public string Username { get; set; }
public string Groupname { get; set; }
public int Priority { get; set; }
}
modelBuilder.Entity<Radusergroup>(entity =>
{
//entity.HasNoKey();
entity.ToTable("radusergroup");
entity.HasIndex(e => e.Username)
.HasName("username");
entity.Property(e => e.Groupname)
.IsRequired()
.HasColumnName("groupname")
.HasMaxLength(64)
.HasDefaultValueSql("''''''");
entity.Property(e => e.Priority)
.HasColumnName("priority")
.HasColumnType("int(11)")
.HasDefaultValueSql("'1'");
entity.Property(e => e.Username)
.IsRequired()
.HasColumnName("username")
.HasMaxLength(64)
.HasDefaultValueSql("''''''");
});