So i am trying to create a table with a foreign key, but it always says that it cannot find the foreign key. heres the code:
public class Tecnologies
{
[Key]
public int TecId { get; set; }
[Required]
public String Name { get; set; }
}
this one works, then i try to create this one:
public class UserTecnologies
{
[Key]
public int UserTecId { get; set; }
[ForeignKey("Id")]
public UserProfile User { get; set; }
[ForeignKey("TecId")]
public virtual Tecnologies Tecnology { get; set; }
[Required]
public int Rating { get; set; }
}
and it gives me the error :
The ForeignKeyAttribute on property 'Tecnology' on type 'ESW_CloddOffice.Models.UserTecnologies' is not valid. The foreign key name 'TecId' was not found on the dependent type 'ESW_CloddOffice.Models.UserTecnologies'. The Name value should be a comma separated list of foreign key property names.
The names are correct, what am i missing ?
Okay, i found what i was doing wrong. Heres the correct code:
public class UserTecnologies
{
[Key]
public int UserTecId { get; set; }
[ForeignKey("UserProfile")]
public virtual int UserProfileId { get; set; }
[ForeignKey("Tecnology")]
public virtual int TecnologyId { get; set; }
public virtual Tecnologies Tecnology { get; set; }
public virtual UserProfile UserProfile { get; set; }
[Required]
public int Rating { get; set; }
}
Was creating the foreign key the wrong way .
The ForeignKey attribute requires that an actual property on the entity match the name you pass in. It doesn't just tell EF what to call the key at the database level.
You either need to actually add a TecId property:
public int TecId { get; set; }
Or use fluent configuration, instead:
modelBuilder.Entity<UserTechnologies>()
.HasRequired(c => c.Technology)
.WithMany()
.Map(m => m.MapKey("TecId"));
Related
So I try to create some ASP.NET project with EF Core.
I want to set propert of one entity as primary key and foreign key to another entity. The relationship is 0..1 - 1. I use DataAnnotations:
public class OfficeAssignment
{
[Key, ForeignKey("InstructorID")]
public int InstructorID { get; set; }
public Instructor Instructor { get; set; }
public string Location { get; set; }
}
But I keep getting column InstructorID as PK and InstructorID1 as FK... Any ideas, why EF behaves like that and how can I achieve my goal?
You should follow convention over configuration as much as you can. An OfficeAssignment entity should have an OfficeAssignmentId PK, like this:
public class OfficeAssignment
{
public int OfficeAssignmentId { get; set; }
//Notice that Id does not have an uppercase D
public int InstructorId { get; set; }
public string Location { get; set; }
public Instructor Instructor { get; set; }
}
However, if you don't want to follow normal conventions, the name of the property that goes in the ForeignKey attribute is the opposite of where it's declared:
public class OfficeAssignment
{
[Key, ForeignKey("Instructor")]
public int InstructorId { get; set; }
public string Location { get; set; }
public Instructor Instructor { get; set; }
}
And, if you want to keep it compile-time safe:
public class OfficeAssignment
{
[Key, ForeignKey(nameof(Instructor))]
public int InstructorId { get; set; }
public string Location { get; set; }
public Instructor Instructor { get; set; }
}
It's enough to set primary key attribute([Key]) in the OfficeAssignment class and in Instructor class we need to set such attribute:
[InverseProperty("Instructor")]
on collection of CourseAssignments. That will work as desired.
I'm trying to add some entities using EntityFramework.
I need the same model as in image
I created two classes:
public class PriceOfDish
{
[Key]
public virtual List<Dish> Dish_ID { get; set; }
[Key]
public DateTime DateTime { get; set; }
public decimal Price { get; set; }
}
public class Dish
{
[Key]
public int Dish_ID { get; set; }
public string DishName { get; set; }
public string Description { get; set; }
public virtual FoodCategory FoodCategory_ID { get; set; }
public virtual Feature Feature_ID { get; set; }
public virtual ICollection<OrderedDishes> Orders { get; set; }
}
Using FluentAPI trying to set primary keys:
builder.Entity<PriceOfDish>()
.HasKey(t => new {t.Dish_ID, t.DateTime});
During updating DB i get error message: "The property 'Dish_ID' cannot be used as a key property on the entity 'testFOef.PriceOfDish' because the property type is not a valid key type. Only scalar types, string and byte[] are supported key types.".
But why? Can you explain this to me? Thanks for any help
Like the error say you are using a List<Dish> as a type for your primary key. You must use an scalar type (value type) as usual or byte[].
For you, the solution is to create a proprerty Dish_ID with int type.
public class PriceOfDish
{
[Key]
public int Dish_ID { get; set; }
[Key]
public DateTime DateTime { get; set; }
[ForeignKey("Dish_Id")]
public virtual Dish Dish { get; set; }
public decimal Price { get; set; }
}
you're trying to set a List<Dish> Dish_ID as a key of a table.
But this is not supported. It doesn't make much sense either. list of Dishes that will have a single Price? I think you want to put int Dish_ID there
I have two classes that relate to one another (one-to-many) and I thought I had the properties setup correctly, but when I run the Update-Database command for my migration, I get the following error:
Introducing FOREIGN KEY constraint
'FK_dbo.ParentEnrollment_dbo.CellGroup_CellGroupID' on table
'ParentEnrollment' may cause cycles or multiple cascade paths. Specify
ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN
KEY constraints. Could not create constraint or index. See previous
errors.
My two classes:
[Table("CellGroup")]
public class CellGroup : BaseEntity
{
public Guid AcademicYearID { get; set; }
[ForeignKey("AcademicYearID")]
public virtual AcademicYear AcademicYear { get; set; }
public Guid LeaderID { get; set; }
[ForeignKey("LeaderID")]
public virtual Parent Leader { get; set; }
public Guid PreviousGroupID { get; set; }
[ForeignKey("PreviousGroupID")]
public virtual CellGroup PreviousGroup { get; set; }
public string Name { get; set; }
public int MaximumSize { get; set; }
public virtual ICollection<ParentEnrollment> Parents { get; set; }
}
and
[Table("ParentEnrollment")]
public class ParentEnrollment : BaseEntity
{
public Guid ParentID { get; set; }
[ForeignKey("ParentID")]
public virtual Parent Parent { get; set; }
public Guid AcademicYearID { get; set; }
[ForeignKey("AcademicYearID")]
public virtual AcademicYear AcademicYear { get; set; }
public bool FirstTimeEnrolling { get; set; }
public string HSLDAAccountNumber { get; set; }
public DateTime HSLDARenewalDate { get; set; }
public string CurrentChurch { get; set; }
public string CurrentChurchContact { get; set; }
public string CurrentChurchPhone { get; set; }
public Guid CellGroupID { get; set; }
[Required]
[ForeignKey("CellGroupID")]
public virtual CellGroup CellGroup { get; set; }
public bool VolunteerBuyOut { get; set; }
public Guid VolunteerPositionID { get; set; }
[ForeignKey("VolunteerPositionID")]
public virtual VolunteerPosition VolunteerPosition { get; set; }
public string VolunteerPositionNotes { get; set; }
public virtual ICollection<StudentEnrollment> StudentEnrollments { get; set; }
}
I only have the Parents property on the CellGroup class so I can easily access the list of enrollments in that cell group. I tried to remove the property to see if it cleared up the warning/error, but it did not. Can someone spot where I have gone wrong with my model(s)?
This error says that you cannot introduce a foreign key from table ParentEnrollment to table CellGroup that has cascading delete enabled, because this will create multiple cascade paths, which is not allowed on SQL Server.
According to the code you posted both tables have relations to a table Parent as well as AcademicYear, which are on non nullable FK columns, so EF will enable cascading on delete by default. With another FK from ParentEnrollment to CellGroup there would be multiple cascade paths, e.g. Parent to CellGroup to ParentEnrollment and Parent to ParentEnrollment, and this is causing your error. Removing the Parent property won't solve this because there still is the same cascading path problem starting from table AcademicYear.
So you have to disable cascading delete for your foreign key, which has to be done using Fluent API in your DbContext like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ParentEnrollment>()
.HasRequired(m => m.CellGroup)
.WithMany(m => m.Parents)
.HasForeignKey(m => m.CellGroupID)
.WillCascadeOnDelete(false);
}
So i am working with entity code firest and i have a user class that looks like this:
public class User
{
[Key]
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime LastModified { get; set; }
}
I am trying to make a "friends table" and no matter what i come up with I get an error on the db creation. This is what I Currently have in the Friends Class:
public class Friend
{
[Key, ForeignKey("User")]
public virtual User MyUser { get; set; }
[Key,ForeignKey("User")]
public virtual User MyFriend { get; set; }
public bool IsAccepted { get; set; }
}
this is the error i get:
The ForeignKeyAttribute on property 'MyUser' on type 'Core.Model.Friend' is not valid. The foreign key name 'User' was not found on the dependent type 'Core.Model.Friend'. The Name value should be a comma separated list of foreign key property names.
What am I missing?
You need to use the Column attribute. Normally I would use something like this:
public class Friend
{
[Key]
[Column(Order = 0)]
public int MyUserId { get; set; }
[Key]
[Column(Order = 1)]
public int MyFriendId { get; set; }
[ForeignKey("MyUserId")]
public virtual User MyUser { get; set; }
[ForeignKey("FriendId")]
public virtual User MyFriend { get; set; }
public bool IsAccepted { get; set; }
}
I'm not sure what would happen if you map the Column attribute directly to the navigation property. You can try it if you like and see what happens.. but the above generally works for me.
Alternatively, if you change to use fluent mapping, you can do something like this:
HasKey(u => new { u.MyUserId , u.MyFriendId });
I'm using Entity Framework 4.3 code-first with Oracle. I'm getting the following error:
System.InvalidOperationException : The ForeignKeyAttribute on property 'WidgetSequence' on type 'WidgetDistributor.WidgetEntity' is not valid. The foreign key name 'WIDGETSEQUENCE_ID' was not found on the dependent type 'WidgetDistributor.WidgetEntity'. The Name value should be a comma separated list of foreign key property names.
My entities are like this:
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[ForeignKey("WIDGETSEQUENCE_ID")]
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
}
My code seems correct. What have I done wrong, here?
If you don't want to use fluent syntax, there are three other ways of implementing the reference using data annotations (Personally I prefer data annotations as they seem easier to read and are written just above the property they are affecting):
1.1)
Use ForeignKey (with an associated property) - version 1
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[Column("WIDGETSEQUENCE_ID")]
public int WidgetSequenceId { get; set; }
[ForeignKey("WidgetSequenceId")] //Has to be a property name, not table column name
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
}
1.2)
Use ForeignKey (with an associated property) - version 2
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[ForeignKey("Sequence")] //Has to be a property name, not table column name
[Column("WIDGETSEQUENCE_ID")]
public int WidgetSequenceId { get; set; }
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
}
2)
You can also use the InversePropertyAttribute.
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[InverseProperty("WidgetEntities")]
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
public virtual List<WidgetEntity> WidgetEntities { get; set; }
}
ForeignKey attibute expects a property name in your class as the argument but you given the column name. Use fluent mappings.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<WidgetEntity>()
.HasRequired(w => w.Sequence)
.WithMany()
.Map(m => m.MapKey("WIDGETSEQUENCE_ID"));
}
There is a table called Users and it has a primary key called UserID.
There is another table called Directory, and it has a column called UserID which is defined as a foreign key to the Users table.
I'm able to use the ForeignKey annotation to map the foreign key like this:
[ForeignKey("xyzzy")]
public int? UserID { get; set; } // This is a column in the table
public virtual User xyzzy { get; set; } // This is my instance of User