Home > Net >  Why does it create an Id field in the database again in C# coding?
Why does it create an Id field in the database again in C# coding?

Time:11-05

My bll codes:

        {
            return BaseList(filter, x => new KasaL
            {
                Id = x.Id,
                Kod = x.Kod,
                KasaAdi = x.KasaAdi,
                OzelKod1Adi = x.OzelKod1.OzelKodAdi,
                OzelKod2Adi = x.OzelKod2.OzelKodAdi,
                Aciklama = x.Aciklama,
                BelgeNo=x.BelgeNo,
                BenzersizKod=x.BenzersizKod,
                DuzenlemeTarihi=x.DuzenlemeTarihi,
                DuzenleyenKullaniciId=x.DuzenleyenKullaniciId,
                FirmaId=x.FirmaId,
                Internal=x.Internal,
                KaydedenKullaniciId=x.KaydedenKullaniciId,
                KayitTarihi=x.KayitTarihi,
                ListeKodu=x.ListeKodu,
                Sinternal=x.Sinternal,
                TabloAdi=x.TabloAdi,
                TabloKodu=x.TabloKodu,
                Borc = x.KasaHareket.Where(y => y.KasaId == x.Id).GroupBy(y => y.KasaId).DefaultIfEmpty().Select(z => new { Borc = z.Select(a => a.Borc).DefaultIfEmpty(0).Sum() }).FirstOrDefault().Borc,
                Alacak = x.KasaHareket.Where(y => y.KasaId == x.Id).GroupBy(y => y.KasaId).DefaultIfEmpty().Select(z => new { Alacak = z.Select(a => a.Alacak).DefaultIfEmpty(0).Sum() }).FirstOrDefault().Alacak,
                Bakiye = x.KasaHareket.Where(y => y.KasaId == x.Id).GroupBy(y => y.KasaId).DefaultIfEmpty().Select(z => new { Borc = z.Select(a => a.Borc).DefaultIfEmpty(0).Sum() }).FirstOrDefault().Borc - x.KasaHareket.Where(y => y.KasaId == x.Id).GroupBy(y => y.KasaId).DefaultIfEmpty().Select(z => new { Alacak = z.Select(a => a.Alacak).DefaultIfEmpty(0).Sum() }).FirstOrDefault().Alacak
            }).OrderBy(x => x.Kod).ToList();
        } 

My DTO Codes:

public class KasaL : BaseEntity
    {
        public string KasaAdi { get; set; }
        public string OzelKod1Adi { get; set; }
        public string OzelKod2Adi { get; set; }
        public string Aciklama { get; set; }
        public decimal Borc { get; set; } = 0;
        public decimal Alacak { get; set; } = 0;
        public decimal Bakiye { get; set; } = 0;

    } 

My Entity Kasa Codes:

   public class Kasa : BaseEntityDurum
    {
        [Index("IX_Kod", IsUnique = false)]
        public override string Kod { get; set; }
        [Required, StringLength(100), ZorunluAlan("Kasa Adı", "txtKasaAdi")]
        public string KasaAdi { get; set; }
        public long? OzelKod1Id { get; set; }
        public long? OzelKod2Id { get; set; }
        [StringLength(1000)]
        public string Aciklama { get; set; }
        public OzelKod OzelKod1 { get; set; }
        public OzelKod OzelKod2 { get; set; }
        public List<KasaHareket> KasaHareket { get; set; }
    }

My KasaHareket entity code:

   public class KasaHareket : BaseEntityDurum
    {
        [Index("IX_Kod", IsUnique = false)]
        public override string Kod { get; set; }
        [Column(TypeName = "date")]
        public DateTime Tarih { get; set; }
        public int IslemYonu { get; set; }
        public long KasaId { get; set; }
        public long? CariId { get; set; }
        public long? BankaId { get; set; }
        public long? Kasa2Id { get; set; }
        public KartTuru KartTuru { get; set; }
        public CariIslemTuru CariIslemTuru { get; set; }
        public long? BaglantiId { get; set; }
        public DekontTuru DekontTuru { get; set; }
        public string Aciklama { get; set; }
        [Column(TypeName = "money")]
        public decimal Borc { get; set; }
        [Column(TypeName = "money")]
        public decimal Alacak { get; set; }

        public Kasa Kasa { get; set; }
        public BankaHesap Banka { get; set; }
        public Kasa Kasa2 { get; set; }
        public Cari Cari { get; set; }
    }
Id  bigint  Unchecked
Kod nvarchar(20)    Unchecked
FirmaId bigint  Checked
ListeKodu   nvarchar(20)    Checked
TabloKodu   nvarchar(200)   Checked
TabloAdi    nvarchar(200)   Checked
Sinternal   nvarchar(500)   Checked
Internal    int Checked
BenzersizKod    nvarchar(500)   Checked
KaydedenKullaniciId bigint  Checked
KayitTarihi date    Unchecked
DuzenleyenKullaniciId   bigint  Checked
DuzenlemeTarihi date    Checked
BelgeNo nvarchar(MAX)   Checked
Tarih   date    Unchecked
IslemYonu   int Unchecked
KasaId  bigint  Unchecked
CariId  bigint  Checked
BankaId bigint  Checked
Kasa2Id bigint  Checked
KartTuru    tinyint Unchecked
CariIslemTuru   tinyint Unchecked
BaglantiId  bigint  Checked
DekontTuru  tinyint Unchecked
Aciklama    nvarchar(MAX)   Checked
Borc    money   Unchecked
Alacak  money   Unchecked
Durum   bit Unchecked
Kasa_Id tinyint Checked

When the migration is done, the Kasa_Id field is created automatically. When this field is created, collection operations are not performed.

The Kasa_Id field is created automatically. This area should not exist.

Can you help me?

CodePudding user response:

Do you have a property defined in of the base classes I.e. BaseEntity or BaseEnityDurum that has a byte field and/or a [Key] attribute Data Annotation in it /

CodePudding user response:

You may need to clarify which table/entity you are seeing the extra ID in. I'm assuming your "BaseEntityDurum" class has:

[Key]
long Id {get; set;}

declared?

One issue I see if you are referring to the KasaHareket table/entity:

    public Kasa Kasa { get; set; }
    public Kasa Kasa2 { get; set; }

While you do have:

    public long KasaId { get; set; }
    public long? Kasa2Id { get; set; }

You are not nominating these as Foreign Keys. EF's default convention is to name FKs based on the type rather than the property name. So with two references to a Kasa entity it would typically use "Kasa_Id" and "Kasa_Id2". It may have auto-resolved "KasaId" from your entity, but then when it went to map Kasa2, it opted to generate "Kasa_Id".

Try updating the Kasa references to nominate the FKs:

[ForeignKey("KasaId")]
public Kasa Kasa { get; set; }
[ForeignKey("Kasa2Id")]
public Kasa Kasa2 { get; set; }
  • Related