0
I already had a resale table, where I had to add a new field. This new field is a FK of a new table that I created. Then we have: Reseller(Reseller) which receives a FK Discount. In Reseller this field is called Azurediscountgroupid(int?). What goes on is that I Peed the class and when I try to open the resale screen, it gives error saying that there is no field: Azurediscountgroup_id, note that it tries to create a new field. I understand that this is due to wrong mapping. I will list below the classes, since Reseller is very large, removed some fields (irrelevant to have them) and left only PK, FK and virtual to save space here. Discount
public class AzureDiscountGroup : EntityBase
{
public int Id { get; set; }
public string Descricao { get; set; }
public decimal PercDesconto { get; set; }
public virtual ICollection<Reseller> Resellers { get; set; }
}
A model Reseller
public class Reseller : EntityBase
{
public virtual ICollection<WhiteLabel> WhiteLabels { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
public DateTime? AcceptContractDate { get; set; }
public int? AcceptContractCustomerId { get; set; }
public virtual Customer AcceptContractCustomer { get; set; }
public int? AzureDiscountGroupId { get; set; }
public virtual AzureDiscountGroup DiscountGroup { get; set; }
}
Resellermap
public class ResellerMap : CustomEntityTypeConfiguration<Reseller>
{
public ResellerMap()
{
this.ToTable("Reseller");
this.HasKey(p => p.Id);
this.Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(x => x.Name).HasMaxLength(200).IsRequired();
this.Property(x => x.Alias).HasMaxLength(30).IsRequired();
this.Property(x => x.Enabled).IsRequired();
this.Property(x => x.CategoriesIds).HasMaxLength(200).IsOptional();
this.Property(x => x.ResellerMpnId).HasMaxLength(100).IsOptional();
this.Property(x => x.PathServiceContractAzure).HasMaxLength(700).IsOptional();
this.Property(x => x.PathPartnershipContractAzure).HasMaxLength(700).IsOptional();
this.HasOptional<Customer>(c => c.AcceptContractCustomer).WithMany().HasForeignKey(c => c.AcceptContractCustomerId);
this.HasOptional<AzureDiscountGroup>(z => z.DiscountGroup).WithMany().HasForeignKey(z => z.AzureDiscountGroupId);
}
}
Map of Discount
public class AzureDiscountGroupMap : CustomEntityTypeConfiguration<AzureDiscountGroup>
{
public AzureDiscountGroupMap()
{
this.ToTable("AzureDiscountGroup");
this.HasKey(p => p.Id);
this.Property(p => p.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
this.Property(x => x.Descricao).IsRequired().HasMaxLength(50);
this.Property(x => x.PercDesconto).IsRequired();
}
}
What I have to do/fix to work properly?
OBS Note that in Mounted Sql(debug output) it creates a new Field, as you can see
SELECT
.........
[Extent1].[AzureDiscountGroupId] AS [AzureDiscountGroupId],
[Extent1].[AzureDiscountGroup_Id] AS [AzureDiscountGroup_Id],
...........
In Debug Output I took this exception
-- Failed in 13 ms with error: Invalid column name 'Azurediscountgroup_id'.
Closed Connection at 29/03/2019 10:13:55 -03:00
Exception generated by: System.Data.Entity.Core.Entitycommandexecutionexception in Entityframework.SqlServer.dll
how is the entity of the
AzureDiscountGroup
?– Leandro Angelo
@Leandroangelo, you say directly in the database? In the class is as posted above and in the normal database
– pnet
You have yet to map the field
AzureDiscountGroupId
then add an extra line in your mapping class, example:this.Property(x => x.AzureDiscountGroupId);
and the other settings of that field. Why does this happen? because if you do not put one is added per convention and in your case gives error.– novic
@Virgilionovic, in reality the empty Withmany() is that zoou. I did so: (a => a.Reseller) and then it worked. I haven’t answered yet because I need to close something here and then I close my posts
– pnet
But even so its mapping remains wrong...
– novic