Zajęło mi zbyt długo, aby znaleźć rozwiązanie dla scenariusza opisanego poniżej. To, co na pozór wydawało się proste, okazało się dość trudne. Pytanie brzmi:Entity Framework, Code First, Zaktualizuj relację "jeden do wielu" z niezależnymi stowarzyszeniami
Korzystanie z Entity Framework 4.1 (podejście Code First) i "Independent association" w jaki sposób przypisać inny koniec do istniejącej relacji "wiele do jednego" w scenariuszu "odłączonym" (Asp.Net w moim walizka).
Model:
Zdaję sobie sprawę, że za pomocą relacji ForeignKey zamiast niezależnych stowarzyszeń byłaby opcja, ale to było moje preferencje nie mają realizację ForeignKey w moim Poços.
Klient ma jeden lub więcej celów:
public class Customer:Person
{
public string Number { get; set; }
public string NameContactPerson { get; set; }
private ICollection<Target> _targets;
// Independent Association
public virtual ICollection<Target> Targets
{
get { return _targets ?? (_targets = new Collection<Target>()); }
set { _targets = value; }
}
}
cel ma jeden klient:
public class Target:EntityBase
{
public string Name { get; set; }
public string Description { get; set; }
public string Note { get; set; }
public virtual Address Address { get; set; }
public virtual Customer Customer { get; set; }
}
klienta wywodzi się z klasy Person:
public class Person:EntityBase
{
public string Salutation { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set ; }
public string Telephone1 { get; set; }
public string Telephone2 { get; set; }
public string Email { get; set; }
public virtual Address Address { get; set; }
}
klasa PodstawaJednostki zapewnia pewne właściwości wspólne:
public abstract class EntityBase : INotifyPropertyChanged
{
public EntityBase()
{
CreateDate = DateTime.Now;
ChangeDate = CreateDate;
CreateUser = HttpContext.Current.User.Identity.Name;
ChangeUser = CreateUser;
PropertyChanged += EntityBase_PropertyChanged;
}
public void EntityBase_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Id != new Guid())
{
ChangeDate = DateTime.Now;
ChangeUser = HttpContext.Current.User.Identity.Name;
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
public event PropertyChangedEventHandler PropertyChanged;
public Guid Id { get; set; }
public DateTime CreateDate { get; set; }
public DateTime? ChangeDate { get; set; }
public string CreateUser { get; set; }
public string ChangeUser { get; set; }
}
Kontekst:
public class TgrDbContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Target> Targets { get; set; }
public DbSet<ReportRequest> ReportRequests { get; set; }
// If OnModelCreating becomes to big, use "Model Configuration Classes"
//(derived from EntityTypeConfiguration) instead
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().HasOptional(e => e.Address);
modelBuilder.Entity<Customer>().HasMany(c => c.Targets).WithRequired(t => t.Customer);
}
public static ObjectContext TgrObjectContext(TgrDbContext tgrDbContext)
{
return ((IObjectContextAdapter)tgrDbContext).ObjectContext;
}
}
Nie mam wystarczającej liczby punktów rekomendacji, aby opublikować odpowiedź w ciągu najbliższych 8 godzin. Publikuje posty, gdy tylko będzie to możliwe. – Martin
To jest dość trudne Pracuję nad całym blogiem na temat tego problemu (mam prawie gotowe przez dwa miesiące, ale nadal nie jestem w pełni z tego zadowolony). Aktualizacja relacji jeden-do-wielu po wielu stronach jest prawdopodobnie główną przyczyną zaimplementowania stowarzyszeń kluczy zagranicznych (zamiast naprawiania tego strasznego zachowania). –
Zgadzam się. Procedura wydaje się zdecydowanie trudniejsza niż powinna być. Mimo to, EF 4 i 4.1 były znacznymi ulepszeniami w stosunku do poprzednich wersji. Badanie tematów związanych z EF 4.1 jest trudne ze względu na zmiany API między CTP a wersjami wydania, różne podejścia w scenariuszach oderwanych i połączonych, wiele, nadmiernie uproszczonych przykładowych aplikacji, które nie dostarczają wskazówek dla dodatkowej złożoności, która -tier rozwiązań ... – Martin