I have 2 entities:
public class User
{
public int userId { get; set; }
public string name { get; set; }
public Guid userGuid { get; set; }
}
public class Absence
{
public int absenceId { get; set; }
public Guid applicantId { get; set; }
public User applicant { get; set; }
public Guid permitterId{ get; set; }
public User permitter{ get; set; }
...
}
AbsencesConfiguration:
this.HasRequired(u => u.Applicant).WithMany().HasForeignKey(d => d.ApplicantId);
this.HasRequired(u => u.Permitter).WithMany().HasForeignKey(d => d.PermitterId);
I'd like a Fluent API mapping between the two classes, but it gives this error message:
Blockquote\tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ApplicantId' on entity 'Absences' does not match the type of property 'UserId' on entity 'User' in the referential constraint 'Absences_Applicant'.
I think this is because EF try to join the two tables with the UserId of the User entity and not with UserGuid column. I thought I would make these two columns of Absence entity unique, but how I can map them together?
Thanks in advance.
The problem is your User primary key is an int, but your foreign key is a Guid.
You need to alter either your User class to have a guid for userId:
public Guid userId { get; set; }
or, update your Absence class to use an int:
public int applicantId { get; set; }
Related
I am trying to create a relationship between two tables but keep getting the following error:
The ForeignKeyAttribute on property 'CallLogId' on type
'Ylp.Web.ParkingApi.DataLayer.Entities.ApiCallLogDetailEntity' is not
valid. The navigation property 'ApiCallLog' was not found on the
dependent type
'Ylp.Web.ParkingApi.DataLayer.Entities.ApiCallLogDetailEntity'. The
Name value should be a valid navigation property name.
DbContextMapping:
modelBuilder.Entity<ApiCallLogDetailEntity>()
.HasRequired<ApiCallLogEntity>(p => p.ApiCallLog);
Primary table:
[Table("ApiCallLog")]
public class ApiCallLogEntity
{
[Key, Column(Order = 0)]
public string CallLogId { get; set; }
[Key, Column(Order = 1)]
public string UserId { get; set; }
[Required]
public string CallFilterId { get; set; }
[Required]
public DateTime LastUpdated { get; set; }
[Required]
public int Count { get; set; }
public virtual ICollection<ApiCallLogDetailEntity> Details { get; set; }
}
foreign table:
[Table("ApiCallLogDetail")]
public class ApiCallLogDetailEntity
{
[ForeignKey("ApiCallLog")]
public string CallLogId { get; set; }
[Required]
public string PrametersHashCode { get; set; }
[Required]
public DateTime LastUpdated { get; set; }
public ApiCallLogEntity ApiCallLog { get;}
}
The foreign key must refer to the whole primary key of the parent table. In your parent table you have a composite primary key which includes CallLogId and UserId. The message is confusing, but this can be part of the error. Is it really necessary to include the UserId in the PK?
Another error is that you have not defined the PK in the dependent table. If the UserId is also necessary on the PK, include it in the dependent table, and make it part of the FK.
I have two entity models, an Account and User and I am having difficulties implement foreign keys in the dependant model (User). As I am developing an Azure Mobile Service app I need to use the Entity Data interface which provides an 'Id' key field by default.
public class Account : EntityData
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AccountId { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string EmailAddress { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string SecurityQuestion { get; set; }
[Required]
public string SecurityAnswer { get; set; }
[Required]
public bool IsBusiness { get; set; }
public virtual User User { get; set; }
public virtual Business Business { get; set; }
}
public class User : EntityData
{
[Key, Column(Order=1)]
public virtual string Id { get; set; }
[Key, Column(Order=2), ForeignKey("Account")]
public int AccountId { get; set; }
public int UserId { get; set; }
[Required]
public string Forename { get; set; }
[Required]
public string Surname { get; set; }
public virtual Account Account { get; set; }
}
My issue occurs when I specify I want to find 'AccountId' Entity Framework interprets it as 'Account' table, 'Id' column.
Output from Code Migrations:-
User_Account_Source: : Multiplicity is not valid in Role
'User_Account_Source' in relationship 'User_Account'. Because the
Dependent Role properties are not the key properties, the upper bound
of the multiplicity of the Dependent Role must be '*'.
User_Account_Target_User_Account_Source: : The types of all properties
in the Dependent Role of a referential constraint must be the same as
the corresponding property types in the Principal Role. The type of
property 'AccountId' on entity 'User' does not match the type of
property 'Id' on entity 'Account' in the referential constraint
'User_Account'.
Any insight would be highly appreciated!
The reason why EF understands it is one-to-many relationship instead one-to-one is because you are composing your PKs with the Id property, wich is not a FK.In one-to-one relationships one end must be principal and second end must be dependent. Principal end is the one which will be inserted first and which can exist without the dependent one. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal. When configuring one-to-one relationships, Entity Framework requires that the primary key of the dependent also be the foreign key, otherwise EF doesn't see it as one-to-one relation.
public class Account
{
[Key]
public int Id { get; set; }
public virtual User User{ get; set; }
}
public class User
{
[Key, ForeignKey("Account")]
public int AccountId { get; set; }
public virtual Account Account{ get; set; }
}
If you think about that, it makes sense,otherwise, the below records could happen:
Accounts
Id
11111111
22222222
Users
Id AccountId
12rr 11111111
22tt 11111111
I have a problem with a navigation property not being loaded. I have this same setup with all my other entities, but this is using a property that isnt a natural FK (Number) and wont cascade, that will be handled by a trigger.
Expression<Func<DivisionBracketGameParticipant, object>>[] includes2 = {
q => q.DivisionWinnerBracketGame,
q => q.DivisionLoserBracketGame
};
var test = _divisionBracketGameParticipantsRepository.GetMany(includes2,
q =>
q.DivisionBracketGame.DivisionBracket.Division.
EventId == eventId);
Database Schema
DivisionBracketGame
Id
Number
DivisionBracketGameParticipant
Id
DivisionBracketGameId -> Id
DivisionBracketGameWinnerNumber -> Number
DivisionBracketGameLoserNumber -> Number
Entities
[Table("DivisionBracketGame", Schema = "GrassrootsHoops")]
public class DivisionBracketGame : BaseEntity
{
public int Id{ get; set; }
public int Number { get; set; }
[InverseProperty("DivisionBracketGame")]
public virtual ICollection<DivisionBracketGameParticipant> DivisionBracketGameParticipants { get; set; }
[InverseProperty("DivisionWinnerBracketGame")]
public virtual ICollection<DivisionBracketGameParticipant> DivisionWinnerBracketGameParticipants { get; set; }
[InverseProperty("DivisionLoserBracketGame")]
public virtual ICollection<DivisionBracketGameParticipant> DivisionLoserBracketGameParticipants { get; set; }
}
[Table("DivisionBracketGameParticipant", Schema = "GrassrootsHoops")]
public class DivisionBracketGameParticipant : BaseEntity
{
public int Id{ get; set; }
public virtual int DivisionBracketGameId { get; set; }
public virtual int? DivisionWinnerBracketGameNumber { get; set; }
public virtual int? DivisionLoserBracketGameNumber { get; set; }
[ForeignKey("DivisionBracketGameId")]
public virtual DivisionBracketGame DivisionBracketGame { get; set; }
[ForeignKey("DivisionWinnerBracketGameNumber")]
public virtual DivisionBracketGame DivisionWinnerBracketGame { get; set; }
[ForeignKey("DivisionLoserBracketGameNumber")]
public virtual DivisionBracketGame DivisionLoserBracketGame { get; set; }
}
EF will not create relation to Number because it is not a primary key. Primary key is of your DivisionBracketGame is Id so both DivisionWinnerBracketGame and DivisionLoserBracketGame are targeting Id (not Number).
One-to-many relation demands that column in principal table is unique - in your case the column should be a Number. This is possible in database by either using primary key from principal table or by using unique index on that column. EF doesn't support unique indexes / candidate keys so the only way to build one-to-many relation in EF is through primary key of principal table.
The FK value is used to get related value so at the moment it probably looks for records with wrong value because it uses a wrong column.
Below are my domain entities
public class User
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime? DateOfBirth { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string EmailAddress { get; set; }
public RoleType RoleType { get; set; }
}
public class Role
{
public int Id { get; set; }
public string Name { get; set; }
}
I have made RoleType as a complex type (to acheive enum mapping). So I could use something like context.Users.FirstOrDefault(u => u.RoleType.Value == (long)RoleTypes.Admin)
RoleTypes.Admin is an enum mapping to the Role entity
public class RoleType
{
public int Value { get; set; }
// And all the implicit operators to map with enum
}
And then I have created a mapping using fluent api
public class RoleTypeMapping : ComplexTypeConfiguration<RoleType>
{
public RoleTypeMapping()
{
Property(r => r.Value)
.HasColumnName("RoleId"); // To make sure that in RoleType property of User EF entity maps to an int column [RoleId] in database (table [Users])
}
}
Using fluent-api, I want to create a foreign key association in [Users] table for [Users].[RoleId] referencing [Role].[Id]. Please can anyone provide me guidance to acheive this
I tired adding a property of type Role and creating a mapping through fluent-api, but EF creates another column Role_Id and makes it the foreign key. I want the existing [RoleId] column (complex type) to be the foreign key
It is not possible. If you want to have association with Role table you must abandon your enum-like approach and define Users entity like:
public class User
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime? DateOfBirth { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string EmailAddress { get; set; }
public Role Role { get; set; }
}
First of all relations are not enums and complex types cannot contain navigation properties (as well as foreign keys).
Is it possible to map an Entity with one identity index that auto increments and a foreign key linking it to another table?
public class Item
{
public int ItemID { get; set; }
[StringLength(20)]
public string Barcode { get; set; }
[StringLength(50)]
public string Name { get; set; }
[StringLength(50)]
public string Description { get; set; }
public decimal Price { get; set; }
[ForeignKey("ItemCategory")]
public string CatID { get; set; }
public virtual ItemCategory ItemCategory { get; set; }
}
public class ItemCategory
{
// This should be the identity index
public int ItemCategoryID { get; set; }
// This should be the foreign key
public string CatID { get; set; }
public string Name { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
I saw this answer - should I configure my tables with modelbuilder?
Foreign key in Item must point to primary key in ItemCategory. FKs in EF behave in exactly same way as in databases. It means that FK must point to property with unique values in the principal entity. The problem is that EF doesn't support unique index / constraint so the only way to achieve uniqueness is primary key.
Because of that you cannot point your FK to CatID unless it is part of primary key but in such case you will have composite key containing both ItemCategoryID and CatID and your Item class will have to contain both of them to form correct FK.