This question is locked. New answers and comments are not allowed.
i try to associate one to one and one to many relationship with tables and it corrects on MysqlDb, but when i call data with UserRepository which derives from OpenAccessBaseRepository<User>, i got an null property which types UserCity, in fact, UserCityId is not null in same instance, also same with other relationship types. (i read the documentation, i tried different method (e.i. IsManaged()) but i couldn't find the solution)
For Example; User.UserCity = new UserCity() is not work, also, User.UserLogins.Add doesnt work because there are null refference,
public
class
User
{
public
User()
{
UserLogins =
new
List<UserLogin>();
}
public
int
UserId {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
int
UserCityId {
get
;
set
; }
public
virtual
UserCity UserCity {
get
;
set
; }
public
virtual
IList<UserLogin> UserLogins {
get
;
set
; }
}
public
class
UserLogin
{
public
Guid UserSession {
get
;
set
; }
public
string
Provider {
get
;
set
; }
public
int
UserId {
get
;
set
; }
public
virtual
User User {
get
;
set
; }
}
public
class
UserCity
{
public
int
UserCityId {
get
;
set
; }
public
string
Name {
get
;
set
; }
}
///// FluentModelMetadataSource.cs
protected
override
IList<MappingConfiguration> PrepareMapping()
{
var conf =
new
List<MappingConfiguration>();
var userMapping =
new
MappingConfiguration<User>();
userMapping.MapType(m =>
new
{
UserId = m.UserId,
Name = m.Name,
UserCity = m.UserCity,
UserCityId = m.UserCityId,
UserLogins = m.UserLogins
}).ToTable(
"users"
);
userMapping.HasProperty(p => p.UserId).IsIdentity(KeyGenerator.Autoinc);
userMapping.HasAssociation(p => p.UserCity)
.HasConstraint((p, c) => p.UserCityId == c.UserCityId)
.ToColumn(
"UserCityId"
)
.IsRequired();
conf.Add(userMapping);
var usercityMapping =
new
MappingConfiguration<UserCity>();
usercityMapping.MapType(m =>
new
{
UserCityId = m.UserCityId,
Name = m.Name
}).ToTable(
"cities"
);
usercityMapping.HasProperty(p => p.UserCityId).IsIdentity(KeyGenerator.Autoinc);
conf.Add(usercityMapping);
var userloginMapping =
new
MappingConfiguration<UserLogin>();
userloginMapping.MapType(m =>
new
{
UserSession = m.UserSession,
Provider = m.Provider,
User = m.User,
UserId = m.UserId
}).ToTable(
"userlogins"
);
userloginMapping.HasProperty(p => p.UserSession).IsIdentity(KeyGenerator.Guid);
userloginMapping.HasAssociation(p => p.User)
.WithOpposite(c => c.UserLogins)
.HasConstraint((p, c) => p.UserId == c.UserId)
.ToColumn(
"UserId"
)
.IsRequired();
conf.Add(userloginMapping);
return
conf;
}
//// SAMPLE CALLS
public UserRepository Users { get; set; }
public UserCityRepository UserCities { get; set; }
public UserLoginRepository UserLogins { get; set; }
public UserCityRepository UserCities { get; set; }
public UserLoginRepository UserLogins { get; set; }
public
List<User> Profile()
{
foreach
(var data
in
UserLogins.GetAll()) UserLogins.Delete(data);
foreach
(var data
in
Users.GetAll()) Users.Delete(data);
foreach
(var data
in
UserCities.GetAll()) UserCities.Delete(data);
UserCity usrc =
new
UserCity { Name =
"Istanbul"
};
UserCities.AddNew(usrc);//adding correctly
User usr =
new
User
{
UserCity = usrc,
Name =
"mustafa"
};
Users.AddNew(usr);//adding correctly
var users = Users.GetAll().ToList();//users are getting but their 'UserCity' properties are null problem is here, 'UserCityId' is not nul
UserLogin usrl =
new
UserLogin
{
UserSession = Guid.NewGuid(),
Provider =
"google"
,
UserId = users.FirstOrDefault().UserId
};
UserLogins.AddNew(usrl);//adding correctly
users = Users.GetAll().ToList();//Users.FirstOrDefault().UserLogins is NULL, so problem is here
return
users;
}