Natomiast mem teraz trochę inny problem.
Entities in 'ApplicationDbContext.Addresseses' participate in the 'Addresses_Suplier' relationship. 0 related 'Addresses_Suplier_Target' were found. 1 'Addresses_Suplier_Target' is expected.
W tych modelach:
public class Addresses
{
public int Id { get; set; }
public string Country { get; set; } // panstwo
public string PostalCode { get; set; } // kod pocztowy
public string City { get; set; } // miasto
public string StreetName { get; set; } // pelna nazwa ulicy
public string FlatNumber { get; set; } // numer mieszkania
public virtual Customer Customer { get; set; }
public virtual Suplier Suplier { get; set; }
}
public class Suplier
{
public int Id { get; set; }
public string CompanyName { get; set; } // nazwa firmy
public ICollection<Addresses> Address { get; set; } // jej adresy
}
A w fluent mapping mam tak:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// ustalenie kluczy dla poszczególnych modeli
modelBuilder.Entity<Addresses>().HasKey(a => a.Id);
modelBuilder.Entity<Product>().HasKey(a => a.Id);
modelBuilder.Entity<ProductCategory>().HasKey(a => a.Id);
modelBuilder.Entity<NutritionalValue>().HasKey(a => a.Id);
modelBuilder.Entity<Customer>().HasKey(a => a.Id);
modelBuilder.Entity<Order>().HasKey(a => a.Id);
modelBuilder.Entity<OrderDetail>().HasKey(a => a.Id);
modelBuilder.Entity<Suplier>().HasKey(a => a.Id);
// definicj relacji pomiedzy modelami
modelBuilder.Entity<Suplier>().HasMany(s => s.Address);
modelBuilder.Entity<Product>().HasMany(p => p.Category);
modelBuilder.Entity<Product>().HasMany(p => p.Supliers);
modelBuilder.Entity<Product>().HasMany(p => p.OrderDetails);
modelBuilder.Entity<Product>().HasRequired(p => p.NutritionalValues);
modelBuilder.Entity<ProductCategory>().HasMany(p => p.Products);
modelBuilder.Entity<OrderDetail>().HasMany(od => od.Products);
modelBuilder.Entity<OrderDetail>().HasRequired(od => od.Order);
modelBuilder.Entity<Order>().HasRequired(o => o.OrderDetail);
modelBuilder.Entity<Order>().HasRequired(o => o.Customer);
modelBuilder.Entity<Order>().HasMany(o => o.Products);
modelBuilder.Entity<Customer>().HasMany(c => c.Addresses);
modelBuilder.Entity<Customer>().HasMany(c => c.Orders);
modelBuilder.Entity<Addresses>().HasRequired(a => a.Customer);
modelBuilder.Entity<Addresses>().HasRequired(a => a.Suplier);
base.OnModelCreating(modelBuilder);
}
I nie wiem czemu to wywala podczas rejestracji użytkownika poprzez AccountController, którego rejestruje sobie w POSTowej akcji
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser()
{
UserName = model.Email,
Email = model.Email,
Customer = new Customer()
{
FirstName = model.FirstName,
LastName = model.LastName,
Age = uint.Parse(model.Age),
Gender = model.Gender,
PhoneNumber = model.PhoneNumber,
Addresses = new Collection<Addresses>()
{
new Addresses()
{
Country = model.Country,
City = model.City,
PostalCode = model.PostalCode,
StreetName = model.StreetName,
FlatNumber = model.FlatNumber
}
}
}
};
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
Bardzo proszę o pomoc :)