c# - Entity Framework Relationship Error -


i following error when using entity framework:

unable determine principal end of association between types 'xxx.domain.entities.usersettings' , 'xxx.domain.entities.user'.  principal end of association must explicitly configured using either  relationship fluent api or data annotations. 

here 2 entity classes:

public class user {     [key, column("un")]     public string username { get; set; }     public int level { get; set; }      public virtual usersettings usersettings { get; set; } }  public class usersettings {     [key]     public string username { get; set; }     public int activerefresh { get; set; }      [foreignkey("username")]     public virtual user user { get; set; } } 

i not sure how resolve error. stuck database design can't update fix issue. there way using fluent api these associations working?

a user can have usersettings object. relationship desired.

it looks need 1 zero-or-one relationship

// configure primary key user modelbuilder.entity<user>()      .haskey(t => t.username);   // map one-to-zero or 1 relationship  modelbuilder.entity<usersettings>()      .hasrequired(t => t.user)      .withoptional(t => t.usersettings); 

this not tested! remove annotations entity classes. link fluent api relationships in comment above has more examples of different kinds of relationship.


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -