【问题标题】:Unable to track an instance of type in .Net core?无法在 .Net 核心中跟踪类型的实例?
【发布时间】:2020-07-26 16:25:41
【问题描述】:

其中一张表出现以下错误。该表没有主键。如何处理? 我正在尝试向表中添加新行。

错误

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

程序.cs

 var CommonDataZipInfo = new CommonDataZipInfo()
 {
      ManuscriptNum = ManuscriptNum,
      ZipfileName = Path.GetFileName(fileName),
      AddDate = DateTime.Now
  };
  context.CommonDataZipInfo.Add(CommonDataZipInfo);
  context.SaveChanges();

【问题讨论】:

标签: c# entity-framework .net-core entity-framework-core


【解决方案1】:

我在这里遇到了同样的问题。这些是我的代码:

[...]

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; }
}

并删除上下文中的 HasNoKey:

[...]

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("''''''");
});

[...]

这可能会解决它。

【讨论】:

    【解决方案2】:

    EF实体必须需要一个主键才能操作,并且该键可以存在或不存在于表中。

    根据我的经验(这是一种解决方法),我正在尝试使用几列找到唯一键并在模型中定义它。也许你可以使用keyless entity types

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我只是在表中添加了一个主键。首先,您将表中的 id 字段添加为 int 并将“Is Identity”设置为 yes。然后使用 Microsoft SQL Management Studio,右键单击 id 行并选择“设置主键”。然后运行 ​​Scaffold-DbContext 生成合适的上下文类

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,这里的各种答案帮助了我。只需添加我的最终解决方案。

        正如其他人指出的那样,当表没有主键时就会出现问题。 如果您没有选择向您的表添加键或更改您的上下文(如 Huander 所建议) 另一种选择是执行原始 sql,如下所示:

        using (var context = new YourContext())
        {
            var result = context.YourTable.FromSqlRaw("SELECT something FROM something");
        }
        

        【讨论】:

        • 嗯,这只会返回一个int(# 受影响的记录),而不是数据。没用。
        • 是的,你是对的@GertArnold 我更新了我的答案以适用于 SELECT 语句
        • 而且Database.FromSqlRaw 不存在。这是一个DbSet 方法。在发布之前测试代码真的很有帮助。
        • 我的错。更新的代码对我有用。
        • 现在尝试使用没有密钥的实体。
        猜你喜欢
        • 1970-01-01
        • 2019-10-28
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 2017-06-30
        • 2021-05-04
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多