c# - Using Entity Framework with existing database error: "Invalid column" despite data annotation -


I am working with FK barriers with an existing database and I am with WebAP. I am using NET MVC4 template. I am trying to make a model like this

  namespace My Model {{Table ("Risk")} Public Square Risk {{Key] [Column ("Risk ID")] Public Long ID {Received; Set; } Public string description {get; Set; } {Intake of public int intensity; Set; } Public string status {get; Set; } The phenomenon of public int {get; Set; } Get public int grade {}; Set; } [Column ("created")] created public user profile {receive; Set; } Created public date time {receive; Set; There is account model from the template including UserProfile, User ID, Email and Password. [Table ("UserProfile")] Public class user profile {[key] [Database generated attribute (database generated option. Ident)] public entry user {receive; Set; } Public String Email {get; Set; }}  

When I try and run the following query -

  var exposure = (h in db.Risks // where x.SomeField == Ordering some values, H. CREATADEDDD descending selection h). Skip (pages - 1) * Pages). Move (page size);  

I get an error: System.Data.SqlClient.SqlException: Invalid column name 'UserProfile_UserId'.

I had already flagged EF for my column annotation how to look for it. It seems that whatever called "_UserId" is the column name.

created is a navigation property for the navigation properties you have [Column] attribute can not be applied. To configure the name of the foreign key column in the database, you need to use the Fluent API. With its model it is not possible with only data annotation:

  modelbuilder.Entity < Risk & gt; () .HasOptional (r = & gt; r.CreatedBy) // or with HasRequired .Many (). Map (M => M. Mamke ("Made")); // This is the FK column name  

Alternatively, you can present the foreign key as the property in your model and then use the annotation:

< Pre> [table ("risk")] public square risk {[key] [column ("risk id")] get public long id { Set; } // ... [column ("created")] public int? Built-in BIID {Received; Set; } // or if necessary, [foreign key ("created")] made public user profile {received; Set; }}

Comments