Merhaba arkadaşlar,
Ben Net6 projesi üzerinde EF Core 6 sürüm kullanıyorum. E-ticaret sipariş yazılımı modellemeye çalışıyorum. Codefirst ile projeyi geliştirdim, Varyasyonlar modelini eklerken tıkandım.
Biri IdentityUser nesnesi olmak üzere 5 tane Modelim var(Siparisler,Urunler,Varyasyonlar,Fotograflar,AppUser)
Siparisler içinde Urunler var ve UrunId ile Urunler tablosuna bağlı.
Urunler içinde Fotograflar var ve FotografId ile Fotograflar tablosuna bağlı.
Fotograflar içinde bire çok ilişkiyi belirtmek için public virtual Urunler List'i var.
Bu şekilde herşey düzgün çalışıyordu, fakat ben Varyasyonlar tablosunu ekleyince iş karıştı.
Siparisler tablosu aynı kaldı
Urunler içinde Fotograflar var ve FotografId ile Fotograflar tablosuna bağlı.
Urunler içinde Varyasyonlar var ve VaryasyonIds list'i ile Varyasyonlar tablosuna bağlı.
Fotograflar içinde bire çok ilişkiyi belirtmek için public virtual Urunler ve Varyasyonlar List'i var.
Varyasyonlar içinde bire çok ilişkiyi belirtmek için public virtual Urunler ve Fotograflar List'i var.
Paket Yönetici Konsoldan Add-migration ve update-database komutlarını çalıştırınca Introducing FOREIGN KEY constraint 'FK_Varyasyonlar_Fotograflar_FotografId' on table 'Varyasyonlar' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors. hatasını alıyorum.
Yapmak istediğim şey :
Bir ürün sadece bir fotograf alır, ama birden fazla varyasyon alabilir. Her varyasyonda bir fotograf alır. Fotografların dosyalarını sha256 ile kodlayıp kaydettim, varyasyon fotografları aynı olursa boşuna dublicate dosya ve database kaydı oluşmasın diye. Bu yüzden aynı FotografId numarasına sahip birden fazla ürün ve varyasyon olabilir.
Kodlar:
//
Hello friends,
I am using EF Core 6 version on Net6 project. I'm trying to model e-commerce ordering software. I developed the project with Codefirst, I got stuck adding the Variations model.
I have 5 Models, one of which is IdentityUser object(Orders,Products,Variations,Photographs,AppUser)
Orders have Items in them and are linked to the Items table by ItemId.
There are Photographs in the Products and they are linked to the Photographs table with PhotoId.
There is a public virtual Products List to indicate one-to-many relationships within photos.
This way everything was working fine, but when I added the Variations table it got messy.
Orders table remained the same
There are Photographs in the Products and they are linked to the Photographs table with PhotoId.
There are Variations in the Products and are linked to the VariationIds list and the Variations table.
There is a public virtual List of Products and Variations to indicate one-to-many relationships within photos.
There is a public virtual List of Products and Photos to specify one-to-many relationships within variations.
Introducing FOREIGN KEY constraint 'FK_Varyasyonlar_Fotograflar_FotografId' on table 'Variations' may cause cycles or multiple cascade paths when you run Add-migration and update-database commands from Package Manager Console. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors. i am getting the error.
What I want to do:
A product only takes one photo, but can have multiple variations. Each variation takes a photograph. I coded and saved the files of the photos with sha256, so that if the variation photos are the same, there would be no duplicate file and database records. Therefore, there may be more than one product and variation with the same FotoId number.
Codes:
public class Siparisler : BaseEntity
{
[Display(Name = "Birim Fiyat")]
public decimal BirimFiyat { get; set; }
[Display(Name = "Para Birimi")]
public string ParaBirimi { get; set; }
public int SiparisDurumId { get; set; }
public virtual SiparisDurumlari SiparisDurumlari { get; set; }
public int UrunId { get; set; }
public virtual Urunler Urunler { get; set; }
public int UyeId { get; set; }
public virtual AppUser Uyeler { get; set; }
public override void Build(ModelBuilder builder)
{
builder.Entity<Siparisler>(entity =>
{
entity
.Property(p => p.ZanaatkarFiyat)
.HasPrecision(18, 4);
});
builder.Entity<Siparisler>(entity =>
{
entity
.Property(p => p.BirimFiyat)
.HasPrecision(18, 4);
});
builder.Entity<Siparisler>(entity =>
{
entity
.HasOne(p => p.Uyeler)
.WithMany(p => p.Siparisler)
.HasForeignKey(p => p.UyeId)
.OnDelete(DeleteBehavior.Cascade);
});
builder.Entity<Siparisler>(entity =>
{
entity
.HasOne(p => p.Urunler)
.WithMany(p => p.Siparisler)
.HasForeignKey(p => p.UrunId)
.OnDelete(DeleteBehavior.Restrict);
});
builder.Entity<Siparisler>(entity =>
{
entity
.HasOne(p => p.SiparisDurumlari)
.WithMany(p => p.Siparisler)
.HasForeignKey(p => p.SiparisDurumId)
.OnDelete(DeleteBehavior.Cascade);
});
}
}
public class Urunler : BaseEntity
{
public Urunler()
{
Fotograflar = new Fotograflar();
}
[Display(Name = "Ürün Adı")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz..!")]
[DataType(DataType.Text)]
[MaxLength(500, ErrorMessage = "{0} en fazla 500 karakter olabilir")]
public string UrunAdi { get; set; }
[Display(Name = "SKU")]
[DataType(DataType.Text)]
public string Sku { get; set; }
[Display(Name = "Yeni Fiyat")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz..!")]
public decimal YeniFiyat { get; set; }
public int UyeId { get; set; }
[ForeignKey("Fotograflar")]
public int FotografId { get; set; }
[ForeignKey("Varyasyonlar")]
List<int> VaryasyonIds { get; set; }
public virtual Fotograflar Fotograflar { get; set; }
public virtual AppUser Uyeler { get; set; }
public virtual ICollection<Varyasyonlar> Varyasyonlar { get; set; }
public virtual ICollection<Siparisler> Siparisler { get; set; }
public override void Build(ModelBuilder builder)
{
builder.Entity<Urunler>(entity =>
{
entity
.Property(p => p.UrunAdi)
.HasMaxLength(500);
entity
.Property(p => p.Sku)
.HasMaxLength(100);
entity
.Property(p => p.YeniFiyat)
.HasPrecision(18, 4);
});
builder.Entity<Urunler>(entity =>
{
entity
.HasOne(p => p.Uyeler)
.WithMany(p => p.Urunler)
.HasForeignKey(p => p.UyeId)
.OnDelete(DeleteBehavior.Cascade);
});
}
}
using KurumsalWebCoreEntity.Entity.Enums;
using KurumsalWebCoreEntity.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using KurumsalWebCoreEntity.Enums;
namespace KurumsalWebCoreEntity
{
public class Varyasyonlar : BaseEntity
{
public Varyasyonlar()
{
Fotograflar = new Fotograflar();
}
[Display(Name = "Varyasyon Türü")]
[DataType(DataType.Text)]
public string VaryasyonTuru { get; set; }
[Display(Name = "Varyasyon Adı")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz..!")]
[DataType(DataType.Text)]
[MaxLength(500, ErrorMessage = "{0} en fazla 500 karakter olabilir")]
public string VaryasyonAdi { get; set; }
[Display(Name = "SKU")]
[DataType(DataType.Text)]
public string Sku { get; set; }
[Display(Name = "Yeni Fiyat")]
[Required(ErrorMessage = "{0} alanı boş bırakılamaz..!")]
public decimal YeniFiyat { get; set; }
[DataType(DataType.Text)]
public string ParaBirimi { get; set; }
[ForeignKey("Fotograflar")]
public int FotografId { get; set; }
public virtual Fotograflar Fotograflar { get; set; }
public virtual List<Urunler> Urun { get; set; }
public override void Build(ModelBuilder builder)
{
builder.Entity<Varyasyonlar>(entity =>
{
entity
.Property(p => p.VaryasyonAdi)
.HasMaxLength(500);
entity
.Property(p => p.Sku)
.HasMaxLength(100);
entity
.Property(p => p.YeniFiyat)
.HasPrecision(18, 4);
});
}
}
public partial class Fotograflar : BaseEntity
{
public string Url { get; set; }
public string Hash { get; set; }
//public int VaryasyonlarId { get; set; }
public virtual List<Urunler> Urun { get; set; }
public virtual List<Varyasyonlar> Varyasyonlar { get; set; }
public override void Build(ModelBuilder builder)
{
builder.Entity<Fotograflar>(entity =>
{
});
}
}