11

Mam następujących klas:EF 4.1 błąd integralności Więzy

public class Person 
{ 
    [Key] 
    public Guid Id { get; set; } 

    [Required] 
    public string FirstName { get; set; } 

    [Required] 
    public string LastName { get; set; } 

    [Required] 
    public string Email { get; set; } 

    public virtual Survey Survey { get; set; } 
} 

public class Survey 
{ 
    [Key] 
    public Guid Id { get; set; } 

    public bool IsFinished { get; set; } 

    public virtual ICollection<UserAnswer> UserAnswers { get; set; } 

    public virtual Person Person { get; set; } 
} 

public class UserAnswer 
{ 
    [Key] 
    public Guid Id { get; set; } 

    public Guid SurveyId { get; set; } 
    public virtual Survey Survey { get; set; } 

    public Guid QuestionId { get; set; } 
    public virtual Question Question { get; set; } 

    public Guid AnswerId { get; set; } 
    public virtual Answer Answer { get; set; } 
} 

w moim datacontext Mam zdefiniowane:

modelBuilder.Entity<Survey>().HasRequired(s => s.Person).WithOptional(); 
modelBuilder.Entity<Survey>().HasMany(s => s.UserAnswers).WithRequired(a => a.Survey).HasForeignKey(a => a.SurveyId).WillCascadeOnDelete(false); 

Czy ktoś może mi powiedzieć co robię źle?

Aktualizacja:

Kiedy wykonać ten kod:

var surveyRepository = new SurveyRepository(); 
foreach (var userAnswer in userAnswers) 
{ 
    survey.UserAnswers.Add(userAnswer); 
} 
surveyRepository.InsertOrUpdate(survey); 
surveyRepository.Save(); 

pojawia się następujący błąd:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

+0

Jakiego błędu dostałeś i kiedy? –

Odpowiedz

6

Proszę spróbować w ten sposób

var surveyRepository = new SurveyRepository(); 
foreach (var userAnswer in userAnswers) 
{ 
    **userAnswer.SurveyId = Survey.Id;** 
    survey.UserAnswers.Add(userAnswer); 
} 
surveyRepository.InsertOrUpdate(survey); 
surveyRepository.Save(); 
+1

W moim przypadku jest to problem wielu do wielu. Istnieją jednostki "Business" i "Category". Chcę, aby każda firma miała wiele "Kategorii", w jaki sposób mogę je dołączyć, używając ich "CategoryID"? – Shimmy

0

Podczas definiowania modelu, nie należy określić Właściwości EF Referencyjnego Id będą je tworzyć w bazie danych.

Zmień model UserAnswer do

public class UserAnswer 
{ 
    [Key] 
    public Guid Id { get; set; } 

    public virtual Survey Survey { get; set; } 

    public virtual Question Question { get; set; } 

    public virtual Answer Answer { get; set; } 
} 
+0

Nie, to nie to, mam inne zajęcia, w których użyłem klawiszy i działają dobrze. Usunąłem klucz i wciąż ten sam błąd. Thx choć dla odpowiedzi – Nealv

1

Wiem, że jest późno, ale może to może być przydatne dla kogoś. A co z próbowaniem tego?

var surveyRepository = new SurveyRepository(); 
surveyRepository.InsertOrUpdate(survey); 
foreach (var userAnswer in userAnswers) 
{ 
    survey.UserAnswers.Add(userAnswer); 
} 
surveyRepository.Save(); 

Ostatnio miałem, że „doszło więzów naruszenie integralności ograniczenia: Wartości właściwości definiujące ograniczenia referencyjne nie są zgodne między zleceniodawcą i obiektów zależnych w stosunku.” Błąd kiedy edytujesz podmiotowi dzieci.

To był mój kod na początku:

myAction.ActionUserNameAssignations.Clear(); 

string[] sSelectedValues = CheckBoxListHelper.GetAllIds(collection, "CheckBox_UserName_", true).ToArray(); 
foreach (string userName in sSelectedValues) 
{ 
    ActionUserNameAssignation assignation = new ActionUserNameAssignation { ActionId = myAction.ActionId, UserName = userName }; 
    myAction.ActionUserNameAssignations.Add(assignation); 
} 

db.Actions.Attach(myAction); 
db.ObjectStateManager.ChangeObjectState(myAction, EntityState.Modified); 
db.SaveChanges(); 

I to jest mój kod na końcu, działa bez zarzutu:

db.Actions.Attach(myAction); 
db.ObjectStateManager.ChangeObjectState(myAction, EntityState.Modified); 

myAction.ActionUserNameAssignations.Clear(); 

string[] sSelectedValues = CheckBoxListHelper.GetAllIds(collection, "CheckBox_UserName_", true).ToArray(); 
foreach (string userName in sSelectedValues) 
{ 
    ActionUserNameAssignation assignation = new ActionUserNameAssignation { ActionId = myAction.ActionId, UserName = userName }; 
    myAction.ActionUserNameAssignations.Add(assignation); 
} 

db.SaveChanges(); 

Jak widać, różnica jest w zasadzie Mocowanie podmiot do kontekstu na początku. Mam nadzieję, że to pomoże :)

Powiązane problemy