Reputation: 380
In begining i have to say that i don't know if it must be many-to-many relationShip
Lets say i have three classess:
User-detalis, which have some basic user-information like region, city
public class UserDetails
{
public int Id{get;set;}
public string City{get;set;}
public strng Region{get;set;}
}
Next class is UserDetailsDictionary
public class UserDetailsDictionary
{
public int Id{get;set;}
public string Name{get;set;}
public string DetailsType{get;set;}
}
This class will containts values like :
Name = "football", detailsType="interests",
Name = "basketball", detailsType="interests",
Name = "brown", detailsType="eyes",
Name = "green", detailsType="eyes"
and the last one class, should contains information about joining userDetails with Dictionary and Values:
public class JoiningClass
{
public UserDetails UserDetails{get;set;}
public UserDetailsDictionary DictionaryItem{get;set;}
public bool Value{get;set;}
}
And values in this class should be like :
(1,1,0)
(1,1,1)(first user like basketball)
(1,2,0)(first user does not have brown eyes)
(2,1,1) (second user likes football)
And in the code i would like to check value from two ways, i mean:
UserDetailsDictionary type;
...
var val1 = _dbContext.UserDetails.Where(x=> x.DictionaryItem.Id == type.Id).Value;
and from the other ways
UserDetails ud;
...
var allDetailsOfUser = _dbContext.UserDetailsDictionary.ToArray();
and also, let's say i want to find all users(userDetails) with brown eyes:
var dictionaryItem = _dbContext.UserDetailsDictionary.Where(x=>x.Name == "eyesColor").SingleOrDefault();
var userDetails = _dbContext.JoiningClass...?
How should i create these classess? How to create correcrt relations to entity framework (core 2.0) in DbContext class?
Upvotes: 2
Views: 371
Reputation: 1631
Let me know if this works for you
I will suggest you to normalized these table as
1.] First Model and Table Columns
public class UserDetails
{
public int UserId{get;set;}
public string UserName{get;set;}
public string City{get;set;}
public strng Region{get;set;}
}
2.] Second Model And Table Columns
public class UserDetailsDictionary
{
public int DictId{get;set;}
public string InterestName{get;set;}
public string DetailsType{get;set;}
}
3.] Third Table to store data
public class UserData
{
public int UserId{get;set;}
public int DictonaryId{get;set;}
public bool _value {get;set;}
}
I assume data is stored in database as you showing in your question
(1,1,0)
(1,1,1)(first user like basketball)
(1,2,0)(first user does not have brown eyes)
(2,1,1) (second user likes football)
Now Lets take one of your example
(2,1,1) (second user likes football)
User With ID 2 have
Blockquote
interest in Football (interest with id 1)
Now from api you can return an anonymous type
var m =(from data in _dbContext.UserData
Join details in _dbContext.UserDetails on data.UserID equals details.UserID
Join dict in _dbContext.UserDetailsDictionary on data.DictionaryID equals dict.DictID
//where Condition if required
select new {
UserName=details.UserName,
Interest= dict.IntersetName,
//_value=true/false
}
).ToList();
Instead of Annonymous Type you can also return storngly Type Model as Json
Upvotes: 1