Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EF does not recognize my SQL server table's Primary key.

$
0
0

I have a Model which has code: 

 public partial class RsbfileDetail
    {
        public string FileUrl { get; set; }
        public string Filename { get; set; }
        public string CaseId { get; set; }
        public string FileType { get; set; }
        [Key]
        public int FileReferenceId { get; set; }
        public DateTime? CreatedDate { get; set; }
        public DateTime? UpdatedDate { get; set; }
        public string Status { get; set; }
        public string FileId { get; set; }
        public string Preset { get; set; }
        public string TranscodeCallbackUrl { get; set; }
        public string FileTranscodeJobId { get; set; }
        public string TranscriptionCallbackUrl { get; set; }
        public string JobDefinition { get; set; }
        public string TranscriptionJobId { get; set; }
    }

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.'"

I have set [Key] attribute in my Model as well. But still I have this error at highlighted code snippet.  

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);
                }
            }
            else
            {
                Console.WriteLine("No trm file in destination folder");
            }

What am I doing wrong? Please let me know.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>