Foregin key relationship error - asp.net-mvc

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.

Related

DevExpress MVC Update members while their orders change by year

I'm working on an MVC application in DevExpress, I have a table that has members I would like to changes the orders or add orders by year but the members would stay the same, so I don't have to re-enter all the members every year just update prices for each member and if the member doesn't have ordered it would show 0
What I have tried so far is I have a master-detail grid and I have the model created
public class AppYear
{
public int AppYearId { get; set; }
public DateTime AppDateTime { get; set; }
public ICollection<MemberBenefit> MemberBenefits { get; set; }
}
public class Dependent
{
public int DependentId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
public int MemberId { get; set; }
public Member Member { get; set; }
public int MemberBenefitId { get; set; }
public MemberBenefit MemberBenefit { get; set; }
}
public class MemberBenefit{
public int MemberBenefitId { get; set; }
[Column(TypeName = "Money")]
public decimal Tmhhea { get; set; }
[Column(TypeName = "Money")]
public decimal Tmna { get; set; }
public int AppYearId { get; set; }
public AppYear AppYear { get; set; }
public ICollection<Dependent> Dependents { get; set; }
}
I don't know if the relation is correct in database models, but I need an idea on how to change only the member benefits and keeping the dependent the same when you change the year a new the member benefit would default to 0

ForeignKey error on composite PK model class

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; }
}

how to create foreign key use migration code first (one to many relationship)

I have 2 classes:
public class Retete
{
public Retete() { }
public int ID { get; set; }
public string Nume { get; set; }
public string Categorie { get; set; }
public string Grupa { get; set; }
public string Descriere { get; set; }
public string Ingrediente { get; set; }
public string Preparare { get; set; }
//Configure 1 to many relationship
//Foreign Key
[ForeignKey("GrupaIndivizi")]
public int GrupaID { get; set; }
[ForeignKey("GrupaID")]
public virtual GrupaIndivizi GrupaIndivizi { get; set; }
}
and
public class GrupaIndivizi
{
public GrupaIndivizi(){}
[Key]
public int GrupaID { get; set; }
public string NumeGrupa { get; set; }
public virtual ICollection<Retete> Retetes { get; set; }
}
and the table that are created
My question is how to make GrupaID from Retetes table to be a foreign Key?
One way to make a foreign key in entity framework is: beside the Id of the key you need to have an object of that type. Check if this works.
[Table("Retete")]
public class Retete
{
public Retete() { }
public int ID { get; set; }
public string Nume { get; set; }
public string Categorie { get; set; }
public string Grupa { get; set; }
public string Descriere { get; set; }
public string Ingrediente { get; set; }
public string Preparare { get; set; }
//Configure 1 to many relationship
//Foreign Key
[ForeignKey("GrupaIndivizis")]
public int GrupaIndiviziID { get; set; }
public GrupaIndivizi GrupaIndivizi { get; set; }
}
I would also recommed to get the tables some more suitable names and don't let EF generate the names. This can be done by the Table attribute.
Your FK didn't generate corectly because he was trying to bind to a GrupaIndivizi table but you have GrupaIndivizis.

EntityFrameWork SaveChanges Error

I am trying to make changes to a database by using Entity Framework (code-first). Everything works fine up until I attempt to save changes, when an error is thrown:
Invalid Column Name BasketID
However, during debugging, looking at the storeDB.BasketItems, the basketitem was actually added.
What am I missing ?
Code:
// Basket is ENTIRELY Empty, add new item and move on.
if (storebasket == null || storebasket.BasketItems.Count == 0)
{
// Add item as is
BasketItem newItem = new BasketItem
{
sellerSKU = basketItem.sellerSKU,
BasketID = basketID,
sellerID = 1,
Quantity = basketItem.Quantity,
Price = basketItem.Price
};
storeDB.BasketItems.Add(newItem);
storeDB.SaveChanges();
}
Database entity:
public class StoreEntities : DbContext
{
public DbSet<Order> Order { get; set; }
public DbSet<OrderItems> OrderItems { get; set; }
public DbSet<Basket> Basket { get; set; }
public DbSet<BasketItem> BasketItems { get; set; }
}
Classes:
public class Basket
{
[Key]
public string BasketID { get; set; }
public virtual IList<BasketItem> BasketItems { get; set; }
public System.DateTime DateCreated { get; set; }
public Guid UserID { get; set; }
}
public class BasketItem
{
[Key]
public int BasketItemID { get; set; }
public virtual string BasketID { get; set; }
[Required]
public int sellerID { get; set; }
[Required]
public string sellerSKU { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public decimal Price { get; set; }
}
OP pasted Edit as Answer:
I have no idea why this fixed the problem, but the program works as expected now
public StoreEntities(): base("storeDBConnectionString")
{
Database.SetInitializer<StoreEntities>(
new DropCreateDatabaseAlways<StoreEntities>());
If you Look at your BasketItem class you set your BasketID as virtual... That causes entity framework not to bind the property to a column. The correct way to do it would be :
public class BasketItem
{
[Key]
public int BasketItemID { get; set; }
public virtual Basket Basket { get; set; }
public string BasketID { get; set; }
[Required]
public int sellerID { get; set; }
[Required]
public string sellerSKU { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public decimal Price { get; set; }
}

MVC3 many-to-many with additional column Examples

which is the best way or if you have seen an example of which is the best way or a way to display to the user a view with this model and if you have and link example will be better
public class Student
{
public int StudentID { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public decimal? Grade { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}

Resources