I've read ten post around this issue but still cannot understand what I am failing: I am stuck on this since two days.
ASPNET MVC5 web app. Code First
Models:
public partial class Category
{
public int ID { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
public bool IsDeleted { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class ISO_Languages
{
public int ID { get; set; }
public string code { get; set; }
public bool IsEnabled { get; set; }
public string name_en { get; set; }
public string name_fr { get; set; }
public string name_it { get; set; }
public string name_de { get; set; }
public string name_es { get; set; }
}
public class CategoryTrans
{
[Key]
[Column(Order = 1)]
public int category_id { get; set; }
[Key]
[Column(Order = 2)]
public int language_id { get; set; }
public string name { get; set; }
}
CategoryTrans has a composite PK based on two FK: Category ID and ISO_Languages ID
I am just trying to correctly reference this architecture in CategoryTrans model, that is something like:
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("ID")]
public virtual Category ID{ get; set; }
[ForeignKey("ID")]
public virtual ISO_Languages ID2{ get; set; }
public string name { get; set; }
The ForeignKeyAttribute on property 'ID' on type 'xyz.Models.CategoryTrans' is not valid. The foreign key name 'ID' was not found on the dependent type 'xyz.Models.CategoryTrans'. The Name value should be a comma separated list of foreign key property names
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("Category")]
public virtual Category catid{ get; set; }
[ForeignKey("ISO_Languages")]
public virtual ISO_Languages languguageid{ get; set; }
public string name { get; set; }
The ForeignKeyAttribute on property 'catid' on type 'xyz.Models.CategoryTrans' is not valid. The foreign key name 'Category' was not found on the dependent type 'xyz.Models.CategoryTrans'. The Name value should be a comma separated list of foreign key property names.
Am I trying to do something impossible?
Try:
public partial class Category
{
public int ID { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
public bool IsDeleted { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<CategoryTrans> CategoryTrans { get; set; }
}
public class ISO_Languages
{
public int ID { get; set; }
public string code { get; set; }
public bool IsEnabled { get; set; }
public string name_en { get; set; }
public string name_fr { get; set; }
public string name_it { get; set; }
public string name_de { get; set; }
public string name_es { get; set; }
public virtual ICollection<CategoryTrans> CategoryTrans { get; set; }
}
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("category_id")]
public virtual Category ID{ get; set; }
[ForeignKey("language_id")]
public virtual ISO_Languages ID2{ get; set; }
public string name { get; set; }
}
Related
I have following model classes
Each Item has 4 languages in perticular languages table
public class Item
{
[Key]
public int Id { get; set; }
public DateTime? ExpireDate { get; set; }
}
public class ItemLanguage
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("Language")]
public int LanguageId { get; set; }
[ForeignKey("Item")]
public int ItemId { get; set; }
public Language Language { get; set; }
public Item Item { get; set; }
}
Each item library has 4 languages in perticular language table
public class ItemLibrary
{
[Key]
public int Id { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
public DateTime? ExpireDate { get; set; }
}
public class ItemLibraryLanguage
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[ForeignKey("Language")]
public int LanguageId { get; set; }
[ForeignKey("ItemLibrary")]
public int ItemLibraryId { get; set; }
public ItemLibrary ItemLibrary { get; set; }
public Language Language { get; set; }
}
Now I want to add items to the ItemLibraryItems table.
It need to save the ItemId and the ItemLibraryId in this table(Not the languageId). When displaying the selected items for the library it needs to show the according to the selected language in login.
public class ItemLibraryItem
{
[Key]
public int Id { get; set; }
[ForeignKey("ItemLibrary")]
public int ItemLibraryId { get; set; }
[ForeignKey("Item")]
public int ItemId { get; set; }
public ItemLibrary ItemLibrary { get; set; }
public Item Item { get; set; }
}
But when I'm trying to create the Controller by using ItemLibraryItem as the model class it gives the error
The entity types 'ItemLibraryItem' and 'ItemLibraryLanguage' cannot share table 'ItemLibraryLanguages' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.
I've been using this tutorial as a guide for code first n:m. I usually go with db first but for a school project I have to use code first...
My 2 classes are as follows:
public class Product
{
public Product()
{
Stocks = new HashSet<Stock>();
ServingUnits = new HashSet<Unit>();
Orders = new HashSet<Order>();
}
public int ProductId { get; set; }
[StringLength(128, MinimumLength = 1)]
[Required]
public string Title { get; set; }
[StringLength(512, MinimumLength = 1)]
public string Description { get; set; }
public int ExperationPeriod { get; set; }
public byte[] Image { get; set; }
[Required]
public DateTime DateCreated { get; set; }
//foreign keys
[Required]
public int StockUnitId { get; set; }
[Required]
public string CreatedById { get; set; }
[Required]
public int ProductStateId { get; set; }
[Required]
public int ProductTypeId { get; set; }
//navigation properties
[ForeignKey("StockUnitId")]
public virtual Unit StockUnit { get; set; }
[ForeignKey("CreatedById")]
public virtual ApplicationUser CreatedBy { get; set; }
[ForeignKey("ProductStateId")]
public virtual ProductState ProductState { get; set; }
[ForeignKey("ProductTypeId")]
public virtual ProductType ProductType { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
public virtual ICollection<Unit> ServingUnits { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
and the other:
public class Unit
{
public Unit()
{
Orders = new HashSet<Order>();
ProductsStock = new HashSet<Product>();
ProductsServe = new HashSet<Product>();
}
public int UnitId { get; set; }
[StringLength(255, MinimumLength = 1)]
[Required]
public string Title { get; set; }
[StringLength(128, MinimumLength = 1)]
public string ShortName { get; set; }
public string Description { get; set; }
[Required]
public double BasisFactor { get; set; }
[Required]
public DateTime DateCreated { get; set; }
//foreign keys
[Required]
public string CreatedById { get; set; }
//navigation properties
[ForeignKey("CreatedById")]
public virtual ApplicationUser CreatedBy { get; set; }
public virtual ICollection<Product> ProductsStock { get; set; }
public virtual ICollection<Product> ProductsServe { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
But the join table doesn't exist in my database. Can anyone see what I'm doing wrong?
Thanks
I have the following tables:
**Table Person**
public long Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public string NameDesportivo { get; set; }
public string Title { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Birthdate { get; set; }
public Nullable<int> CC { get; set; }
public Nullable<int> NIF { get; set; }
public Nullable<bool> Active { get; set; }
public string Natural { get; set; }
public string Nacionality { get; set; }
public string fatherName { get; set; }
public string motherName { get; set; }
public string street { get; set; }
public string numberDoor { get; set; }
public string local { get; set; }
public string parish { get; set; }
public int Id_Contacto { get; set; }
public virtual ICollection<Contact> Contact { get; set; }
public HabLiterarias HabLiterarias { get; set; }
**Table ContactPerson**
public int ContactID
public int PersonId
**Table ContactType**
public long ID { get; set; }
public string Name { get; set; }
public Nullable<bool> Active { get; set; }
public string UserIDModification { get; set; }
public Nullable<System.DateTime> DataModification { get; set; }
public virtual ICollection<Contact> Contact { get; set; }
**Table Contact**
public long ID { get; set; }
public Nullable<long> ContactTypeID { get; set; }
public string ContactValue { get; set; }
public virtual ContactType ContactType { get; set; }
public virtual ICollection<Person> Person { get; set; }
What I'm trying to do is when I create an Person insert more than one contact of that person at the some time: For example Insert type contact mail and it´s value and contact type phone and it's value into the table contact.
Those any one have any ideias. Thank you
I know this question has already been asked repeatedly, but I can't seem to find an answer that will fit my needs:
I have this model:
[Table("AppUser", Schema = "AppUsers")]
public class AppUser
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AppUserID { get; set; } //Primary key
[Required]
public string UserIdentificationNumber { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string Street { get; set; }
public int StreetNo { get; set; }
public string Mobile { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
[Required]
public string Gender { get; set; }
public string AboutMeDescription { get; set; }
[ScaffoldColumn(false)]
public string HashedPassword { get; set; }
[ScaffoldColumn(false)]
public string PasswordSalt { get; set; }
[ScaffoldColumn(false)]
public int? VolunteeringRadius { get; set; }
[ScaffoldColumn(false)]
public float? RankAvg { get; set; }
[ScaffoldColumn(false)]
public bool? IsApproved { get; set; }
[ScaffoldColumn(false)]
public DbGeography Location { get; set; }
[ScaffoldColumn(false)]
public bool? IsEmailApproved { get; set; }
[ScaffoldColumn(false)]
public bool? IsSMSApproved { get; set; }
[ScaffoldColumn(false)]
public int? PrefferedContactMethodID { get; set; } //Foreign key
[ScaffoldColumn(false)]
public int? RegisterationStatusID { get; set; } //Foreign key
[ScaffoldColumn(false)]
public bool? IsActive { get; set; }
[ScaffoldColumn(false)]
public bool? IsFirstLogin { get; set; }
[ScaffoldColumn(false)]
public bool? IsWebRegister { get; set; }
[ScaffoldColumn(false)]
public int? LanguageID { get; set; }
[Required]
[ScaffoldColumn(false)]
public int AppUserTypeID { get; set; } //Foreign key
[Required]
[ScaffoldColumn(false)]
public int CityId { get; set; } //Foreign key
[ScaffoldColumn(false)]
public int? OrganizationID { get; set; } //Foreign key
[ForeignKey("AppUserTypeID")]
public virtual AppUserType AppUserType { get; set; } //Navigation property
[ForeignKey("CityId")]
public virtual City City { get; set; } //Navigation property
[ForeignKey("OrganizationID")]
public virtual Organization Organization { get; set; }
}
which, using entity framework code first approach, renders as a new table in my DbContext and in turn in my SQL Database.
I also have this ViewModel
public class AppUserCreateViewModel
{
[Key]
public int AppUserID { get; set; } //Primary key
public string UserIdentificationNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public int StreetNo { get; set; }
public string Mobile { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
public string Gender { get; set; }
[ScaffoldColumn(false)]
public int AppUserTypeID { get; set; } //Foreign key
[Required]
[ScaffoldColumn(false)]
public int CityId { get; set; } //Foreign key
[ScaffoldColumn(false)]
public int? OrganizationID { get; set; } //Foreign key
public IEnumerable<SelectListItem> AppUserTypes { get; set; } //Navigation property
public IEnumerable<SelectListItem> Cities { get; set; } //Navigation property
public IEnumerable<SelectListItem> Organizations { get; set; }
}
I probably am doing something wrong here, since I want to create a "Create" view from this ViewModel, but to my understanding, I cannot scaffold it, since it will than create an Empty table in the Database (named "AppUserCreateViewModel"). I really have no need in that table (the table I wish to use is "AppUser".
So my questions are:
What should I do if I wish to create a strongly-typed view, by using my viewModel, but without creating a corresponding table in my DB, and to bind this view to my AppUser table?
Should I be using the Repository pattern? Or is it an overkill?
I want the CreateViewModel will render 3 DropDownLists (AppUserTypes, Cities and Organizations) - What's the best way to achieve this?
Thank you very much
I keep getting this error and I do not know why.
The ForeignKeyAttribute on property 'Ward' on type 'BioSheet.Models.BioSheetModel' is not valid. The foreign key name 'WardId' was not found on the dependent type 'BioSheet.Models.BioSheetModel'. The Name value should be a comma separated list of foreign key property names.
public class Ward
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("AddressId")]
[Required]
public virtual Address WardAddress { get; set; }
[ForeignKey("BioSheetId")]
public virtual List<BioSheetModel> BioSheets { get; set; }
[Required]
public String Code { get; set; }
}
public class BioSheetModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public String FirstName { get; set; }
[Required]
public String LastName { get; set; }
public String Email { get; set; }
[ForeignKey("WardId")]
[Required]
public Ward Ward { get; set; }
public String CellPhoneNumber { get; set; }
public String HouseNumber { get; set; }
[Required]
public String DoB { get; set; }
[Required]
public Address Address { get; set; }
public String OtherInformation { get; set; }
public String PreviousCallings { get; set; }
[ForeignKey("TimePeriodId")]
public virtual TimePeriod TimePeriods { get; set; }
public String HomeWard { get; set; }
public Boolean OkToText { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public DateTime TodaysDate { get; set; }
[ForeignKey("EMPId")]
public virtual EDUEMP EduEmp { get; set; }
[ForeignKey("SingId")]
public virtual Sing Singing { get; set; }
[ForeignKey("MissionId")]
public virtual Mission MissionIn { get; set; }
}
Can anyone help me resolve this?
[ForeignKey("WardId")] indicates that the property to use as a foreign key to the Ward table should be the WardId property on the BioSheetModel class.
You're getting the error because you haven't defined a WardId property on the BioSheetModel class.
Add
public int WardId {get; set;}
for a non-nullable/required relationship, or
public int? WardId {get; set;}
for a nullable/optional relationship.