locked
Need my EF core to see already existing primary key in my SQL server table and work with it. RRS feed

  • Question

  • User-1660589204 posted

    I have a SQL server DB table namely "RSBFileDetails", which has a primary key column "[File_Reference_Id]" auto generated int type. When I run my code to enter values in my DB using EF, I get an error: "System.InvalidOperationException: 'Unable to track an instance of type 'RsbfileDetail' because it does not have a primary key. Only entity types with primary keys may be tracked.'"

    My code to enter data is: 

    public async void PostValuesIntoDB()
            {
                DirectoryInfo info = new DirectoryInfo(path);          
                FileInfo[] fileInfos = info.GetFiles("*.trm");
                if (fileInfos != null || fileInfos.Length>0)
                {
                    foreach (FileInfo fileInfo in fileInfos)
                    {
                        RsbfileDetail detail = new RsbfileDetail();
                        detail.Filename = fileInfo.Name;
                        detail.FileUrl = fileInfo.FullName;
                        detail.FileType = fileInfo.Extension;                   
                        detail.CreatedDate = fileInfo.CreationTime;                   
                        await _rSBRepository.Add(detail);
                    }
                }

     Highlighted code creates error message. 

    public class RSBRepository : Repository<RsbfileDetail>, IRSBRepository
        {
            private readonly rsbsrdbContext _dbContext;        
            public RSBRepository(rsbsrdbContext dbContext) : base(dbContext)
            {
                _dbContext = dbContext;           
            }
            Task<RsbfileDetail> IRSBRepository.UploadFiles(string filename)
            {
                throw new NotImplementedException();
            }
    
            public async Task<string> Add(RsbfileDetail rsbfileDetail)
            {
                string dbresult = string.Empty;
                _dbContext.Add(rsbfileDetail);
                var response= await _dbContext.SaveChangesAsync();
                dbresult = response.ToString();
    
                return dbresult;
            }
            public void GetNewFileFromFolderToDB()
            {
                List<RsbfileDetail> result1 = new List<RsbfileDetail>();
                result1 = _dbContext.RsbfileDetails.Where(j => (j.Filename == "sample.txt")).ToList();       
                Console.WriteLine(result1);
            }        
        }

    How can I get my code to insert values into my database. Please help me with this 

    Monday, November 16, 2020 9:38 AM

All replies

  • User1120430333 posted

    Maybe, you should be taking a basic course on how to use EF. This is something that one should know how to do using the basics.

    Monday, November 16, 2020 10:48 PM
  • User-189459990 posted

    In my test, the EF Core works fine that it doesn’t trigger the “System.InvalidOperationException” and data can be inserted into the table in succeed.<o:p></o:p>

    How did you generate the DbContext class? It would be better if you can provide related code.<o:p></o:p>

    And is there similar code in the generated OnModelCreating method?<o:p></o:p>

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RsbfileDetail>(entity =>
        {
            entity.HasKey(e => e.FileReferenceId);
            entity.ToTable("RSBFileDetails");
            // … 
          } 
    }
    
    Tuesday, November 17, 2020 7:39 AM