Use 2 key to make relations on Entity Framework - entity-framework

This example structure of the old project.
public class Customs
{
[Key,Required, Column(Order = 0)]
public string DeclarationNo { get; set; }
[Key, Required, Column(Order = 1)]
public string ReceivedControlNo { get; set; }
[Key, Required, Column(Order = 2)]
public string MasterBL { get; set; }
public string CustomsDetail {get;set;}
public virtual CustomsManifest Manifest { get; set; }
}
public class CustomsManifest
{
[Key,Required, Column(Order = 0)]
public string ReceivedControlNo { get; set; }
[Key,Required, Column(Order = 1)]
public string MasterBL { get; set; }
public string ManifestDetail {get; set; }
public virtual Customs Customs { get; set; }
}
How to make relations with 2 key. is it possible?
the Key I need to join between Customs & CustomsManifest is
ReceivedControlNo
MasterBL
Relationship's zero or one

Related

independent association between two non-relational / unrelated entities

I have a need to obtain a list of non-relational entity values using a navigational property. In the following model for FVariable, I am trying to create a navigation property for maintaining a list of VariableValue. How to define an independent association as depicted in the model of FVariable:
public virtual ICollection<VariableValue> Values { get; set; } ?
The two entities are related through VariableID and DataAttributeCollectionID properties.
Here is the FVariable Entity:
public class FVariable
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int FVariableID { get; set; }
[Index(Unique, Order = 1)]
public int VariableID { get; set; }
[ForeignKey(nameof(F.VariableID))]
public virtual Variable Variable { get; set; }
[Index(Unique, IsUnique = true, Order = 2)]
public int? DataAttributeCollectionID { get; set; }
[ForeignKey(nameof(FVariable.DataAttributeCollectionID))]
public virtual DataAttributeCollection DataAttributeCollection { get; set; }
public virtual ICollection<VariableValue> Values { get; set; }
}
Here is the VariableValue entity:
public class VariableValue
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VariableValueID { get; set; }
[Index(Order = 1)]
public int VariableID { get; set; }
[ForeignKey(nameof(VariableValue.VariableID))]
public virtual Variable Variable { get; set; }
[Index(Order = 2)]
public double VariableValue { get; set; }
public int? DataAttributeCollectionID { get; set; }
[ForeignKey(nameof(VariableValue.DataAttributeCollectionID))]
public virtual DataAttributeCollection DataAttributeCollection { get; set; }
}

Entity Framework Code First: The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical

Database build error:
One or more validation errors were detected during model generation:
Key_Authorities_Source_Key_Authorities_Target: : The number of
properties in the Dependent and Principal Roles in a relationship
constraint must be identical.
Key Class:
[Table("Keys")]
public class Key
{
[Key, Column(Order = 0)]
public int Id { get; set; }
[Key, Column(Order = 1)]
public int OwnedByFId { get; set; }
[Key, Column(Order = 2)]
public int OwnedByUId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[ForeignKey("Id"), Column(Order = 1)]
public virtual ICollection<KeyAuthorities> Authorities { get; set; }
}
Key Authorities Class:
[Table("Key_Auths")]
public class KeyAuthorities
{
[Key, Column(Order = 0)]
public int Id { get; set; }
[Key, Column(Order = 1)]
public int KeyId { get; set; }
public int DoorId { get; set; }
public int VehicleId { get; set; }
public int GateId { get; set; }
}
Why doesn't this set this foreign key?
The error message is actually telling you that the number of properties don't match. This is because your Key class is uniquely identified by 3 properties (the 3 PK that you define: Id, OwnedByFId and OwnedByUId), but you're trying to define a Foreign Key to your Key Class using only the Id.
You must set all the PKs on your foreign class:
[Table("Key_Auths")]
public class KeyAuthorities
{
[Key, Column(Order = 0)]
public int Id { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Id, OwnedByFId, OwnedByUId")]
public int KeyId { get; set; }
public int DoorId { get; set; }
public int VehicleId { get; set; }
public int GateId { get; set; }
}
Notice that I added the data annotation [ForeignKey("Id, OwnedByFId, OwnedByUId")].
// error message is basically telling that you have
// not configured the keys and their order properly in "Keys" table
public class Keys {
[Key, Column(Order = 0)]
public int Key_1 { get; set; }
[Key, Column(Order = 1)]
public int Key_2 { get; set; }
// order is important here as defined in "KeyAuthorities" table
[ForeignKey("KeyAuthorities", Column(Order = 0)]
public int KeyAuthorities_Key_1 { get; set; }
[ForeignKey("KeyAuthorities", Column(Order = 1)]
public int KeyAuthorities_Key_2 { get; set; }
public virtual ICollection KeyAuthorities { get; set; }
}
public class KeyAuthorities {
[Key, Column(Order = 0)]
public int KeyAuthorities_Key_1 { get; set; }
[Key, Column(Order = 1)]
public int KeyAuthorities_Key_2 { get; set; }
}

Code first One way many to many relationship using data annotation

I have two models in my application: Stock and Report. A report can have many stocks and a stock can be used in many reports.
public enum ElectionType
{
MANAGER =1 ,
INSPECTOR ,
BOTH
}
public class Report
{
public Report()
{
Stocks = new List<Stock>();
}
public int ReportID { get; set; }
[Required]
public DateTime ShamsiDate { get; set; }
public int? StockID { get; set; }
[Required]
public ElectionType ElectionType { get; set; }
[ForeignKey("StockID")]
public virtual ICollection<Stock> Stocks { get; set;}
}
public class Stock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StockID { get; set; }
public int FinancialCode { get; set; }
public String NationalCode { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public String FatherName { get; set; }
public String Place { get; set; }
public int StockCount { get; set; }
public int StockPrice { get; set; }
public int StockSize { get; set; }
public int StartStock { get; set; }
public int EndStock { get; set; }
}
I want to create a one way relationship so there is no way to access a Report from a Stock. I have written this code but it doesn't work.
To do it with annotations you will need a junction table:
public class ReportStock
{
[Key, Column(Order = 1)]
public int ReportID { get; set; }
[Key, Column(Order = 2)]
public int StockID { get; set; }
[ForeignKey("ReportID")]
public virtual Report Report { get; set;}
[ForeignKey("StockID")]
public virtual Stock Stock { get; set;}
}
Then change your Report class:
public class Report
{
public Report()
{
ReportStocks = new List<ReportStock>();
}
public int ReportID { get; set; }
[Required]
public DateTime ShamsiDate { get; set; }
[Required]
public ElectionType ElectionType { get; set; }
public virtual ICollection<ReportStock> ReportStocks { get; set;}
}

Using Automapper CreateMap to concatenate elements

I have the following EF classes:
public class Request
{
[Key]
public virtual int RequestID { get; set; }
...
public virtual List<RequestLinked> RequestLinkeds { get; set; }
}
public class RequestLinked
{
[Key, Column(Order = 0)]
[ForeignKey("Request")]
public int RequestID { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("RequestRelated")]
public int RequestRelatedID { get; set; }
public virtual Request Request { get; set; }
public virtual Request RequestRelated { get; set; }
}
I created a DTO like this:
[DataContract]
public class RequestDTO
{
[DataMember]
public int RequestID { get; set; }
...
[DataMember]
public string RequestRelatedIDs { get; set; }
}
Now I would like to use automapper fo fill my DTO object. I need to fill RequestRelatedIDs with all elements from RequestLinkeds.RequestRelatedID by concatenate all elements inside 1 string.
I tried this:
CreateMap<Request, RequestDTO>()
.ForMember(x => x.RequestRelatedIDs, o => o.MapFrom(src => string.Join(", ", src.RequestLinkeds.Select(x => x.RequestRelatedID));
But the Select method is red highlighted: System.Collections.Generic.List< PLATON.Domain.Models.RequestLinked >' does not contain a definition for 'Select'
Why is that not possible to perform a select? An alternative?
Thanks.

Composite key in table manupulation in Entity Framework

I use Entity Framework 5 Code First and have some problem with composite keys.
I have those tables
And this is how i map the entities
public class Product : EntityBase
{
public Product()
{
this.ProductArticles = new List<ProductArticle>();
}
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ProductId { get; set; }
[Key, Column(Order = 1)]
public int PricelistId { get; set; }
public string Description { get; set; }
public string Unit { get; set; }
public string ReportText1 { get; set; }
public string ReportText2 { get; set; }
public bool Standard { get; set; }
public int ProductGroupId { get; set; }
public decimal? Surcharge1 { get; set; }
public decimal? Surcharge2 { get; set; }
public decimal? Surcharge3 { get; set; }
public decimal? Surcharge4 { get; set; }
public decimal PriceMaterialIn { get; set; }
public decimal AdjMaterialIn { get; set; }
public decimal F_PriceMaterialInAdj { get; set; }
public decimal F_AdjMaterial { get; set; }
public decimal F_PriceMaterialOut { get; set; }
public decimal PriceArtisanIn { get; set; }
public decimal F_AdjArtisan { get; set; }
public decimal F_PriceArtisanOut { get; set; }
public decimal F_TotalOut { get; set; }
public decimal F_TotalOutVat { get; set; }
public bool GetPrice { get; set; }
public string Notes { get; set; }
[ForeignKey("ProductGroupId,PricelistId")]
public virtual ProductGroup ProductGroup { get; set; }
[ForeignKey("ProductId,PricelistId")]
public virtual ICollection<ProductArticle> ProductArticles { get; set; }
[ForeignKey("PricelistId")]
public virtual Pricelist Pricelist { get; set; }
}
public class ProductGroup : EntityBase
{
[Key, Column(Order = 0)]
public int ProductGroupId { get; set; }
[Key, Column(Order = 1)]
public int PricelistId { get; set; }
public int OptionalGroupId { get; set; }
public string Prefix { get; set; }
public string Description { get; set; }
public decimal? Surcharge1 { get; set; }
public decimal? Surcharge2 { get; set; }
public decimal? Surcharge3 { get; set; }
public decimal? Surcharge4 { get; set; }
public string ReportText1 { get; set; }
public string ReportText2 { get; set; }
[ForeignKey("OptionalGroupId,PricelistId")]
public virtual OptionalGroup OptionalGroup { get; set; }
[ForeignKey("PricelistId")]
public virtual Pricelist Pricelist { get; set; }
}
But when the context is build i get this message
337,10) : error 3015: Problem in mapping fragments starting at lines
303, 337:Foreign key constraint 'Product_ProductGroup' from table
Product (PricelistId, ProductGroupId) to table ProductGroup
(ProductGroupId, PricelistId):: Insufficient mapping: Foreign key must
be mapped to some AssociationSet or EntitySets participating in a
foreign key association on the conceptual side.
I solved it by redesign the table. Think that composite key are bad design.