问题
Problem: insert / update entity related to a parent with a one-to-many relationship in a disconnected graph scenario.
UPDATE
I found that the problem is just with new UserProfile I add to Brewery, the first insert is done successfully (= EF sees the relationship and creates the correct insert statement)
exec sp_executesql N'insert [dbo].[BreweryUserProfiles]([BreweryId], [UserId])
values (@0, @1)
',N'@0 int,@1 int',@0=12,@1=3
No way till now to have the update working.
---
The parent entity (Brewery) has a one-to-one relation with Rank, and a one-to-many relation with UserProfile. I used a join table for this second relation. I'm able to insert / update the Rank when I insert / update the parent brewery (even if I had to add some code, EF wasn't able to keep the relationship). I'm not able to insert / update an user profile
Reference: I'm using a generic repository + a dbcontextextension as described here: http://www.martinwilley.com/net/code/data/genericrepository.html
DB diagram
POCO
[Table("Breweries")]
public class Brewery : ABrewery
{
//PK
public int BreweryId { get; set; }
//FK
public int RankId { get; set; }
...
//Navigation
public virtual ICollection<BrewMaster> BrewMasters { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
public virtual Country Country { get; set; }
public virtual Rank Rank { get; set; }
}
public class Rank : IRank
{
public int RankId { get; set; }
public int TotalHits { get; set; }
public decimal Value { get; set; }
}
[Table("UserProfiles")]
public class UserProfile : IUserProfile
{
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
...
//Navigation
public virtual List<Email> Emails { get; set; }
public virtual List<Social> Socials { get; set; }
public virtual List<Telephone> Telephones { get; set; }
public virtual Country Country { get; set; }
}
Entity (Brewery) Configuration
HasMany(e => e.UserProfiles)
.WithMany()
.Map(m =>
{
m.MapLeftKey("BreweryId");
m.MapRightKey("UserId");
m.ToTable("BreweryUserProfiles");
});
Property(e => e.RankId)
.IsRequired();
Solution tried
To insert / update Rank, was enough to do that
public void AddOrUpdate(Brewery entity)
{
entity.RankId = entity.Rank.RankId;
_context.AddOrUpdate<Rank>(entity.Rank); //add or update is a dbcontextextension,
//basically just Add or
//Attach + change entity status
_repository.AddOrUpdate(entity);
To update the BreweryUserProfiles I tried to:
if (entity.UserProfiles.Count > 0)
{
entity.UserProfiles = new List<UserProfile>();
foreach (var user in entity.UserProfiles)
{
entity.UserProfiles.Add(GetUserProfile(user.UserId)); //get userprofile from db
}
}
_repository.AddOrUpdate(entity);
or
if (entity.UserProfiles.Count > 0)
{
entity.UserProfiles = new List<UserProfile>();
foreach (var user in entity.UserProfiles)
{
_context.AddOrUpdate<UserProfile>(this.GetUserProfile(user.UserId)); //that basically is the same
}
}
when I tried to do the same for the UserProfile, obviously EF executed an insert / update on UserProfile table (useless .. I need to update BreweryUserProfiles table). If I understood correctly, the Rank is working 'cause I have a FK in Brewery.RankId property, while I don't have (and don't want) a two way navigation between Brewery and UserProfile
I hope my problem is clear, I trid to paste all relevant code, but if needed I can provide more informations. Thanks in advance
来源:https://stackoverflow.com/questions/15050480/ef5-update-disconnected-graph-n-tier-one-to-many