0

I have a c# project with entity framewrok connected with a database. The database has a table with a primary key (auto increment) column. It generated this in the autogenerated file:

public partial class TradingInquiryLog
{
    public int InquiryLogID { get; set; }
    public string TransactionType { get; set; }
    public string TransactionData { get; set; }
    public Nullable<int> CompanyID { get; set; }
    public string AccountName { get; set; }
    public System.DateTime CreateDT { get; set; }
    public string PersonName { get; set; }
}

however when I make a new instance of it,

TradingInquiryLog mLog = new TradingInquiryLog("Inquiry", User.Identity.Name, mCompanyID, TradingHelper.GetTradingInquirySearchResultXML(mCompanies), mDisplayName);

it says im missing a parameter InquiryLogID, however I don't want to set it because it is the primary key auto increment. How can I fix this?

Thanks

2
  • Are you using "Code first" approach in Entity Framework?
    – Fabio
    Oct 6, 2018 at 23:14
  • 4
    If so, in OnModelCreating(ModelBuilder builder) method try to add builder.Entity<TradingInquiryLog>().Property(p => p.InquiryLogID).ValueGeneratedOnAdd();
    – Fabio
    Oct 6, 2018 at 23:16

1 Answer 1

0

Ef core is expecting you to use a convention. Your code will work if you rename the PK field to TradingInquiryLogID. You can also use the data annotations or the fluent API if you want to go those routes as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.