I've got the following :
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
[ForeignKey("UserProfile")]
public int UserProfileID { get; set; }
public UserProfile UserProfile { get; set; }
}
}
public class UserProfile : BaseModel
{
[Key]
public int UserProfileID { get; set; }
[Required]
public String FirstName { get; set; }
[Required]
public String LastName { get; set; }
public String LicenseNumber { get; set; }
public String Position { get; set; }
public bool? Active { get; set; }
[Required]
[ForeignKey("ApplicationUser")]
public String AppUserID { get; set; }
public ApplicationUser ApplicationUser { get; set; }
[ForeignKey("Division")]
public int? DivisionID { get; set; }
public virtual Division Division { get; set; }
public virtual ICollection<Building> Buildings { get; set; }
public virtual ICollection<UserProcess> UserProcesses { get; set; }
public UserProfile()
{
Buildings = new HashSet<Building>();
}
}
I'm getting the error
'UserProfile_ApplicationUser_Target: : Multiplicity is not valid in
Role 'UserProfile_ApplicationUser_Target' in relationship
'UserProfile_ApplicationUser'. Because the Dependent Role properties
are not the key properties, the upper bound of the multiplicity of the
Dependent Role must be '*'."
I don't see what's wrong. This should be a 1:1 relationship. An ApplicationUser has a userProfile and UserProfile always has an applicationuser. Is the way I have it coded making it a 1 to many relationship since multiple user profiles could have an ApplicationUser? I want to maintain the int key on UserProfile.
Related
in my project (Asp.net Core Web Api) I have the tables "Truck" and "UserAccount with a one to many relashionship.
[Table("UserAccount")]
public class UserAccount : BaseClass
{
// Foreign Keys
[ForeignKey(nameof(UserAccountType))]
public int UserAccountTypeId { get; set; }
[ForeignKey(nameof(Gender))]
public int GenderId { get; set; }
[ForeignKey(nameof(Truck))]
public int TruckId { get; set; }
// Properties
public string LastName { get; set; }
public string FirstName { get; set; }
public string UserName { get; set; }
[DataType(DataType.EmailAddress)]
public string Mail { get; set; }
public string Login { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
// Navigation Properties
[IgnoreDataMember]
public virtual Gender Gender { get; set; }
//public virtual Truck Truck { get; set; }
[IgnoreDataMember]
public virtual UserAccountType UserAccountType { get; set; }
public Truck Truck { get; set; }
}
[Table("Truck")]
public class Truck : BaseClass
{
// Foreign Keys
// Properties
[Column(Order = 3)]
public string Name { get; set; }
[DataType(DataType.EmailAddress)]
[Column(Order = 4)]
public string Mail { get; set; }
[Column(Order = 5)]
public string Phone { get; set; }
[Column(Order = 6)]
public string VATNumber { get; set; }
// Navigation Properties
public virtual ICollection<TruckFoodType> TruckFoodTypes { get; set; }
public virtual ICollection<TruckOption> TruckOptions { get; set; }
public ICollection<UserAccount> UserAccounts { get; set; }
}
In the method OnModelCreation into my ApplicationDbContex file I have this to create the one to many relashionship:
modelBuilder.Entity<UserAccount>()
.HasOne<Truck>(u => u.Truck)
.WithMany(t => t.UserAccounts)
.HasForeignKey(u => u.TruckId);
But when I try to populate the UserAccount table I have this error message :
"Merge instruction is in conflict with "FK_User_Account_TruckId". This conflict occurse in the database xxx table dbo.Truck column Id" (Sorry, Comes from a french translation)
I don't hunderstand why.
Can somebody help me?
Thanks
OK, stupid mistake. In some cases the TruckId field from the User Account table can be null. So I added a "?" to this fields like this : public int? TruckId { get; set; } Sorry for inconvenience
When it comes to a single object's foreign key, we would have a property:
SingleMap.cs
[ForeignKey("ApplicationUser")]
public int ApplicationUserID { get; set; }
public ApplicationUser ApplicationUser { get; set; }
How do we map the foreignkey when we have a list of ApplicationUsers?
ManyMap.cs
public ICollection<ApplicationUser> ApplicationUserList { get; set; }
it is usually used this way:
public class ManyMap
{
public int ID {get; set;}
.....
[InverseProperty(nameof(ApplicatonUser.ManyMap))]
public ICollection<ApplicationUser> ApplicationUsers{ get; set; }
}
public class ApplicationUser
{
......
public int ManyMapID { get; set; }
[ForeignKey(nameof(ManyMapID))]
[InverseProperty("ApplicationUsers")]
public ManyMap ManyMap { get; set; }
}
Two Tables One To Many
VehicleStatus {Id , LastVehicleStatusUpdateId}
VehicleStatusUpdated {Id, VehicleStatusId, IsResponse }
I need to make (one to zero or one) to last record in VehicleStatusUpdated ...
LastVehicleStatusUpdateId ==> Navigator To VehicleStatus
like This
How can i make it with Entity Framework Annotation ??!!
public class Vehicle : IEntity<int>
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Number { get; set; }
public virtual VehicleStatus VehicleStatus { get; set; }
}
public class VehicleStatus : IEntity<int>
{
[ForeignKey("Vehicle")]
public int Id { get; set; }
public virtual Vehicle Vehicle { get; set; }
public int? LastVehicleStatusUpdateId { get; set; }
[ForeignKey("LastVehicleStatusUpdateId")]
public virtual VehicleStatusUpdate LastVehicleStatusUpdate { get; set; }
public virtual List<VehicleStatusUpdate> VehicleStatusUpdates { get; set;
}
public class VehicleStatusUpdate : IEntity<int>
{
[ForeignKey("LastVehicleStatus")]
public int Id { get; set; }
public DateTime UpdatedTime { get; set; }
public bool IsResponse { get; set; }
public int VehicleStatusId { get; set; }
[ForeignKey("VehicleStatusId")]
public virtual VehicleStatus VehicleStatus { get; set; }
}
I Tried this i got =>
Unable to determine the relationship represented by navigation property 'VehicleStatus.LastVehicleStatusUpdate' of type 'VehicleStatusUpdate'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
When we start a normal MVC project it comes with Identity.I have some models like Post.cs and Comment.cs
One to many relationship between post and comment
public class Post
{
public int PostId { get; set; }
public string Baslik { get; set; }
public string Icerik { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int CommentId { get; set; }
public string Icerik { get; set; }
public int PostId {get;set;}
public virtual Post Post { get; set; }
}
IdentityModel.cs We have nothing in here but when i click with ctrl on IdentityUser
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
public class IdentityUser : IUser
{
public IdentityUser();
public IdentityUser(string userName);
public virtual ICollection<IdentityUserClaim> Claims { get; }
public virtual string Id { get; set; }
public virtual ICollection<IdentityUserLogin> Logins { get; }
public virtual string PasswordHash { get; set; }
public virtual ICollection<IdentityUserRole> Roles { get; }
public virtual string SecurityStamp { get; set; }
public virtual string UserName { get; set; }
}
Still weird to me for realationship and i clicked with ctrl on IUser
// Summary:
// Minimal interface for a user with a string user key
public interface IUser
{
// Summary:
// Unique key for the user
//
// Returns:
// The unique key for the user
string Id { get; }
string UserName { get; set; }
}
What am I missing? I just want a relationship user-comment and user-post like between Post and Comment models.
I have the following models:
User:
public class User : IEntity, INamedType
{
public virtual int UserId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string UserName { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string FirstName { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public virtual string LastName { get; set; }
[Column(TypeName = "varchar")]
[StringLength(200)]
public virtual string EmailAddress { get; set; }
public int AreaId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string CreatedByUserName { get; set; }
public DateTime CreatedDateTime { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string LastModifiedByUserName { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
public bool Active { get; set; }
//Navigation properties
public virtual Area Area { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
Role:
public class Role : IEntity
{
public int RoleId { get; set; }
[Column(TypeName = "varchar")]
[StringLength(100)]
public string Name { get; set; }
[Column(TypeName = "varchar")]
[StringLength(1000)]
public string Description { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string CreatedByUserName { get; set; }
public DateTime CreatedDateTime { get; set; }
[Column(TypeName = "varchar")]
[StringLength(64)]
public string LastModifiedByUserName { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
//Navigation Properties
public ICollection<User> Users { get; set; }
}
UserRole:
public class UserRole
{
public int UserId { get; set; }
public int RoleId { get; set; }
//Navigation properties
public virtual User User { get; set; }
public virtual Role Role { get; set; }
}
I also have a CustomRoleProvider with this method:
public override string[] GetRolesForUser(string username)
{
var user = _unitOfWork.UserRepository.GetUser(username);
var roles = from r in user.Roles
select r.Name;
if (roles != null)
return roles.ToArray();
else
return new string[] { };
}
so this all works fine until a an entry is added or removed from the UserRole table. For the User this record relates to the new Role when added by adding a UserRole row does not come through in the GetRolesForUser method. Likewise if a UserRole record is removed the Role keeps coming through for the record.
If however an IISReset occurs then all the correct records come through.
Anyone know why this would be happening and how to rectify?
The UserRole entity class conflicts with the join table created by EF for the many to many relationship.
You should either remove the UserRole entity or map the relationships in User and Role to the join entity
public class User : IEntity, INamedType
{
public virtual int UserId { get; set; }
public virtual ICollection<UserRole> Roles { get; set; }
}
public class Role : IEntity
{
public int RoleId { get; set; }
//Navigation Properties
public ICollection<UserRole> Users { get; set; }
}