2016-06-06 9 views
1

ich diesen Code haben eine EntitätEntity Framework Core-Many-to-many-Aktualisierungs-Objekt

public async Task<User> UpdateAsync(User entity) 
{ 
     var userDb = await _context.User.Include(x => x.UserApplications).ThenInclude(xx => 
     xx.Application).Include(x => x.State).Include(x => x.UserProfiles).ThenInclude(x => 
     x.Profile).FirstOrDefaultAsync(x => x.UserName == entity.UserName && x.SecretKey == 
     entity.SecretKey && x.Email == entity.Email); 


    userDb.FirstName = entity.FirstName; 
    userDb.LastName = entity.LastName; 
    userDb.State = await _context.State.FirstAsync(x => x.StateId == entity.State.StateId); 

    userDb.UserProfiles.Clear(); 
    userDb.UserApplications.Clear(); 
    userDb.UserProfiles.AddRange(entity.UserProfiles); //ManyToMany 
    userDb.UserApplications.AddRange(entity.UserApplications); //Many To Many 
    _context.User.Update(userDb); 
    await _context.SaveChangesAsync(); 
    return userDb; 
} 

Generieren von SQL

Ausgeführt DbCommand (8ms) zu aktualisieren [Parameter = [@ p0 = '? ', @ p1 ='? ', @ p14 ='? ', @ p2 ='? ', @ p3 ='? ', @ p4 ='? ', @ p5 ='? ', @ p6 ='? ' , @ p7 = '?', @ p8 = '?', @ p9 = '?', @ p10 = '?', @ p11 = '?', @ p12 = '?', @ p13 = '?', @ p15 = '?', @ p16 = '?', @ p17 = '?', @ p18 = '?', @ p19 = '?', @ p20 = '?', @ p21 = '?'], CommandType = 'Text', CommandTimeout = '30 '] INSERT IN "Anwendung" ("ApplicationId", "ApplicationName") VALUES (@ p0, @ p1); UPDATE "Benutzer" SET "ApiKeyId" = @ p2, "Kommentar" = @ p3, "Erstellungsdatum" = @ p4, "Email" = @ p5, "Vorname" = @ p6, "IsOnLine" = @ p7, "LastLoginDate" = @ p8, "LastName" = @ p9, "Passwort" = @ p10, "SecretKey" = @ p11, "StateId" = @ p12, "Benutzername" = @ p13 WO "UserId" NICHT VON @ p14 ABGEHEND IST; LÖSCHEN VON "UserApplication" WO "UserApplicationId" NICHT VON @ p15; DELETE FROM "UserProfile" WO "UserProfileId" NICHT VON @ p16; DELETE FROM "UserProfile" WO "UserProfileId" NICHT VON @ p17; INSERT IN "UserProfile" ("ProfileId", "UserId") WERTE (@ p18, @ p19) RETURNING "UserProfileId"; INSERT IN "UserProfile" ("ProfileId", "UserId") WERTE (@ p20, @ p21) RETURNING "UserProfileId";

Warum EF eine neue Einheit

(INSERT INTO "Application" ("ApplicationId", "ApplicationName") 
    VALUES (@p0, @p1)) if only i need update the user? 
  • EF Core-Version einsetzen:
  • Betriebssystem 1 RC2: Mac OSX

Grüße

Benutzerklasse

public sealed class User 
{ 
    public int UserId { get; set; } 
    public string SecretKey { get; set; } 
    public ApiKey Apikey { get; set; } 
    public string UserName { get; set; } 
    public string Email { get; set; } 
    public string Comment { get; set; } 
    public string Password { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public DateTime LastLoginDate { get; set; } = DateTime.Now; 
    public DateTime CreationDate { get; set; } = DateTime.Now; 
    public bool IsOnLine { get; set; } = false; 
    public State State { get; set; } = new State(); 
    public List<UserProfile> UserProfiles { get; set; } = new List<UserProfile>(); 
    public List<UserApplication> UserApplications { get; set; } = new List<UserApplication>(); 


} 

Userapplication Klasse

public class UserApplication 
{ 
    public int UserApplicationId { get; set; } 
    public int UserId { get; set; } 
    public User User { get; set; } 
    public int ApplicationId { get; set; } 
    public Application Application { get; set; } 
} 
+2

weil Sie 'Clear' und' AddRange' darauf verwendet haben? – muratgu

+0

mit "Clear" Ich lösche alle Application-Objekt von UserApplications, und die neue Daten einfügen – ngonzalezromero

Antwort

0

Ich habe vor einiger Zeit eine in dieser Situation gewesen.

In meiner Situation geschah die Einfügung, weil das Objekt, das ich hinzufügte, den Primärschlüssel (Id) = 0 hatte. Also EF immer als eine neue Zeile verstanden.

Kann eine Möglichkeit sein.

+0

ich setze jede ID .. :( – ngonzalezromero

1

Wenn Sie

userDb.UserApplications.Clear(); 

Sie klar rufen jeden UserApplication, was für die Application vermutlich das wichtigste ist. Cascade delete entfernt die Application, so dass EF weiß, dass sie es erneut einfügen muss.