Unable to track an instance of type in .Net core?

13,068

Solution 1

I had the same problem here. These are my codes:

[...]

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

[...]

Since you are not creating a key for your table, and sometimes, you can't change the database for some reason. In my case, this is a database which another application uses, so I don't want to compromise that other application, so my solution was simply turn my attribute "username" into a key, like this:

public partial class Radusergroup
{
   [Key]
   public string Username { get; set; }
   public string Groupname { get; set; }
   public int Priority { get; set; }
}

and remove the HasNoKey on your context:

[...]

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

[...]

That will probably solve it.

Solution 2

EF entity must need a primary key to operate and that key can exists or not in table.

From my experience(this is a workaround), I am trying to find a unique key using few columns and define that in model. Maybe you can use keyless entity types.

Share:
13,068
James123
Author by

James123

Updated on June 13, 2022

Comments

  • James123
    James123 almost 2 years

    I am getting below error for one of the tables. the table doesn't have a primary key. How to handle this? I am trying to add new row to the table.

    Error

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

    Program.cs

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