OK, po prostu robisz coś złego. Oprócz komentarza utworzyłem próbkę, która pokazuje, że EF nie tworzy domyślnie duplikatów.
Mam dwie klasy:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public ProductCategory Category { get; set; }
public decimal Price { get; set; }
}
public class ProductCategory
{
public int Id { get; set; }
public string Name { get; set; }
}
jednym kontekście:
public class MyContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductCategory> ProductCategories { get; set; }
public MyContext()
: base("name=MyContext")
{
}
public MyContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<MyContext>(null);
// Table mappings
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<ProductCategory>().ToTable("ProductCategory");
base.OnModelCreating(modelBuilder);
}
}
Wtedy jedna klasa inicjatora (to może dziedziczyć z innych strategii, jeśli chcesz):
public class InitDb<TContext> : DropCreateDatabaseAlways<TContext>
where TContext : DbContext
{
}
The program główny:
static void Main(string[] args)
{
var prodCat = new ProductCategory()
{
Name = "Category 1"
};
var prod = new Product()
{
Name = "Product 1",
Category = prodCat,
Price = 19.95M
};
using (var context = new MyContext())
{
var initializer = new InitDb<MyContext>();
initializer.InitializeDatabase(context);
Console.WriteLine("Adding products and categories to context.");
context.ProductCategories.Add(prodCat);
context.Products.Add(prod);
Console.WriteLine();
Console.WriteLine("Saving initial context.");
context.SaveChanges();
Console.WriteLine("Context saved.");
Console.WriteLine();
Console.WriteLine("Changing product details.");
var initProd = context.Products.Include(x => x.Category).SingleOrDefault(x => x.Id == 1);
PrintProduct(initProd);
initProd.Name = "Product 1 modified";
initProd.Price = 29.95M;
initProd.Category.Name = "Category 1 modified";
PrintProduct(initProd);
Console.WriteLine();
Console.WriteLine("Saving modified context.");
context.SaveChanges();
Console.WriteLine("Context saved.");
Console.WriteLine();
Console.WriteLine("Getting modified product from database.");
var modProd = context.Products.Include(x => x.Category).SingleOrDefault(x => x.Id == 1);
PrintProduct(modProd);
Console.WriteLine();
Console.WriteLine("Finished!");
Console.ReadKey();
}
}
static void PrintProduct(Product prod)
{
Console.WriteLine(new string('-', 10));
Console.WriteLine("Id : {0}", prod.Id);
Console.WriteLine("Name : {0}", prod.Name);
Console.WriteLine("Price : {0}", prod.Price);
Console.WriteLine("CatId : {0}", prod.Category.Id);
Console.WriteLine("CatName : {0}", prod.Category.Name);
Console.WriteLine(new string('-', 10));
}
Wynika to w następujący wyjścia konsoli:
Adding products and categories to context.
Saving initial context.
Context saved.
Changing product details.
----------
Id : 1
Name : Product 1
Price : 19,95
CatId : 1
CatName : Category 1
----------
----------
Id : 1
Name : Product 1 modified
Price : 29,95
CatId : 1
CatName : Category 1 modified
----------
Saving modified context.
Context saved.
Getting modified product from database.
----------
Id : 1
Name : Product 1 modified
Price : 29,95
CatId : 1
CatName : Category 1 modified
----------
Finished!
Również, gdy patrząc w SQL Server Management Studio, rozwiązanie to stworzyło jedynie (i aktualizowany) jednego produktu i jednej kategorii.
Oczywiście, powinieneś pracować z repozytoriami, aby pobrać, zaktualizować i usunąć swoje dane oraz jednostkę pracy. Zostały one pominięte w przykładzie.
Tak więc, jeśli nie pisać żadnego kodu, nie możemy pomóc znacznie dalej :-)
Czy możesz dodać kod, w którym tworzysz dane? Jeśli twoje referencje na twoich zajęciach są poprawne, EF powinno tworzyć wszystkie instancje tylko jeden raz. – Nullius
Tak tworzy wszystkie wystąpienia na raz. W rzeczywistości to jest problem. Pozwól mi ponownie wyjaśnić problem. Załóżmy, że mam instancję produktu, w której znajduje się instancja ProductType. Załóżmy, że instancja produktu już istnieje w przeszłości. Kiedy próbuję zaktualizować tę instancję produktu, EF tworzy zduplikowaną instancję ProductType (co jest przypadkiem, którego nie chcę), jeśli nie ustawię stanu atrybutu ProductType na Unchanged. – Furkan