Entity Framework foreign key not found during migration - c#

I'm experiencing an unexpected error when setting up a migration after adding keys and foreign keys to my data model. I'm using VS2013 Express, with .NET framework 4.5.
When creating a data model for Entity Framework, because the relationship keys between classes aren't what is expected by convention, I'm using data annotations as outlined in the MS Data Developer Center. Here's the class code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BacklogTracker.Models
{
public class WorkOrder
{
[Key]
public string woNum { get; set; }
public string woClosingStatus { get; set; }
[ForeignKey("ID")]
public virtual ICollection<Note> woNotes { get; set; }
[ForeignKey("machSN")]
public virtual Machine woMachine { get; set; }
[ForeignKey("ID")]
public virtual ICollection<Segment> woSegments { get; set; }
}
public class Machine
{
[Key]
public string machSN { get; set; }
public string machLocation { get; set; }
public string machModel { get; set; }
}
public class Segment
{
[Key]
public int ID { get; set; }
public uint segNum { get; set; }
public string segRepair { get; set; }
[ForeignKey("ID")]
public virtual ICollection<Note> segNotes { get; set; }
}
public class Note
{
[Key]
public int ID { get; set; }
public DateTime notetimestamp { get; set; }
public string notestring { get; set; }
}
}
However, when I try to perform a migration after updating the model by performing enable-migrations in the package manager console, I get the following error:
The ForeignKeyAttribute on property 'woMachine' on type
'BacklogTracker.Models.WorkOrder' is not valid. The foreign key name
'machSN' was not found on the dependent type
'BacklogTracker.Models.WorkOrder'. The Name value should be a comma
separated list of foreign key property names.
Why is my foreign key name 'machSN' not being found?

I think you have some errors in your model. Default Code First convention for ForeignKey relationship expected to have declared a foreign key property in the dependend end (WorkOrder) that match with primary key property of the principal end (Machine). It is not necessary that they have the same name, check this link. So, declare a property named machSN in your WorkOrder class:
public class WorkOrder
{
[Key]
public string woNum { get; set; }
public string woClosingStatus { get; set; }
public virtual ICollection<Note> woNotes { get; set; }
public string machSN { get; set; }
[ForeignKey("machSN")]
public virtual Machine woMachine { get; set; }
public virtual ICollection<Segment> woSegments { get; set; }
}
You can find other errors in the woNotes and woSegments navigation properties. In this side of a one-to-many relationship you don't declare a FK, is in the other side, in Note and Segment classes, for example:
public class Note
{
[Key]
public int ID { get; set; }
public DateTime notetimestamp { get; set; }
public string notestring { get; set; }
[ForeignKey("Order)]
public string woNum { get; set; }
public virtual WorkOrder Order{get;set;}
}
Delete also in the Segment class the ForeignKey attribute over segNotes navigation property for the same reasons explained before.
public class Segment
{
[Key]
public int ID { get; set; }
public uint segNum { get; set; }
public string segRepair { get; set; }
public virtual ICollection<Note> segNotes { get; set; }
}

Related

ef-code-first one-to-many relation with tinyint primary key migration fails

I am using Entity Framework Code-First and I am new to that. Here are my models:
public class Tbl_Organization_Type
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte fld_organization_type_id { get; set; }
[MaxLength(100)]
public string fld_organization_type_name { get; set; }
public byte? fld_sort { get; set; }
public ICollection<Tbl_Organization> Tbl_Organization { get; set; }
}
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public long? fld_organization_parent_id_ref { get; set; }
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
When i add ( add-migration personnel_1 -context PersonnelDbContext )
it gives me below error :
The relationship from 'Tbl_Organization.fld_organization_type_id' to
'Tbl_Organization_Type.Tbl_Organization' with foreign key properties
{'fld_organization_type_id_ref' : Nullable} cannot target the
primary key {'fld_organization_type_id' : byte} because it is not
compatible.
Configure a principal key or a set of compatible foreign
key properties for this relationship.
That does not look like a correct model (fld_organization_type_id_ref is not shown or is mistyped). IAC, that field needs to be of the same type as the primary key for Tbl_Organization_Type (byte). Try:
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public byte? fld_organization_type_id_ref { get; set; } // This FK needs to match referenced PK type
// Are you missing or not showing fld_location_id_ref
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
Similarly, you need to show/define the field fld_location_id_ref and match the type with the reference table Tbl_Personnel_Location.

ASP.NET MVC EF Unable to determine the principal end of association

I've encountered some problems with associations while using Entity Framework. Althought there are some other posts about that I couldn't make it working.
public class BaseJobOffer : IEntity
{
[Key, ForeignKey("File")]
public int Id { get; set; }
public string Name { get; set; }
public int FileId { get; set; }
public File File { get; set; }
}
public class File
{
public int Id { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public Enums.FileType FileType { get; set; }
public int JobOfferId { get; set; }
public virtual BaseJobOffer JobOffer { get; set; }
}
And the error says:
There was an error running selected code generator:
'Unable to retrieve metadata for Model.JobOffer'. Unable to determine
the principal end of association between the types
'Model.BaseJobOffer' and 'Model.File'. The principal end of this
association must be explicitly configured using either the
relationship fluent API or data annotations.'
See this article about ForeignKey attribute usage http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
The ForeignKey attribute should be on either the ForeignKey property or the ForeignKeyID property
public class BaseJobOffer : IEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("File")
public int FileId { get; set; }
//or
[ForeignKey("FileId")
public File File { get; set; }
}

Define foreign key for ICollection in Entity Framework model

How to define in Entity Framework 6, which columns will be used to reference data in an ICollection<>?
For example, we have a simple User-Information relation, were i can get all information from a user which updated or created the information:
public class User
{
public int UserId
{
get;
get;
}
// Here is the problem, how to define that EF
// Has to use the CreateUser column
public virtual ICollection<User> User_CreateUser
{
get;
set;
}
// Here is the problem, how to define that EF
// Has to use the UpdateUser column
public virtual ICollection<User> User_UpdateUser
{
get;
set;
}
}
public class Information
{
public int InfoId
{
get;
set;
}
public int CreateUser
{
get;
set;
}
public int UpdateUser
{
get;
set;
}
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("CreateUser")]
public virtual User User_CreateUser
{
get;
set;
}
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("UpdateUser")]
public virtual User User_UpdateUser
{
get;
set;
}
}
But i don't know, how to map this. For the simple foreign-keys it was easy over annotations.
I use database first but generating the models on my own. (Which works pretty good besides the ICollection<>.
Thank you!
You've marked your second foreign key as User but it should be int.
public int UpdateUser { get; set; }
Try this:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace Test
{
public class User
{
public int UserId { get; set; }
public virtual ICollection<User> User_CreateUser { get; set; }
public virtual ICollection<User> User_UpdateUser { get; set; }
}
public class Information
{
public int InfoId { get; set; }
public int CreateUser { get; set; }
public int UpdateUser { get; set; }
[ForeignKey("CreateUser")]
public virtual User User_CreateUser { get; set; }
[ForeignKey("UpdateUser")]
public virtual User User_UpdateUser { get; set; }
}
}

Entity Framework 6 - foreign key issue

I have the following model using EF6, where I'm trying to link a 'ContentArticleHOAsubdivision' entity to a 'SubdivisionHOA':
public partial class ContentArticleHOAsubdivision
{
public int Id { get; set; }
[ForeignKey("ContentArticleHOA")]
public long ContentArticleId { get; set; }
[ForeignKey("SubdivisionsHOA")]
public short SubdivisionId { get; set; }
public virtual ContentArticleHOA ContentArticleHOA { get; set; }
public virtual ICollection<SubdivisionHOA> SubdivisionsHOA { get; set; }
}
public partial class SubdivisionHOA
{
public short Id { get; set; }
public string Name { get; set; }
[ForeignKey("TopTierDivisionHOA")]
public byte TopTierDivisionId { get; set; }
public virtual TopTierDivisionHOA TopTierDivisionHOA { get; set; }
}
I get the error:
The foreign key component 'SubdivisionId' is not a declared property on type 'SubdivisionHOA'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
The 'Id' property in SubdivisionHOA I'm trying to link on needs to be named 'Id' as that's the name of the actual column in the database, so I'm not sure what I need to do?
I've tried adding [InverseProperty("Id")] before the [ForeignKey] attributes but get the same error.
Basicly the foreign key usage in EntityFramework seems like this:
public partial class ContentArticleHOAsubdivision
{
public int Id { get; set; }
...
public virtual ICollection<SubdivisionHOA> SubdivisionsHOAs { get; set; }
}
public partial class SubdivisionHOA
{
public short Id { get; set; }
...
public int ContentArticleHOAsubdivisionId { get; set; }
[ForeignKey("ContentArticleHOAsubdivisionId")]
public virtual ContentArticleHOAsubdivision ContentArticleHOAsubdivision { get; set; }
}
Just based on your code, and I have discard some unknown stuffs.

Relationships in Entity Framework Code First

yesterday I created database in Management Studio and now I want to create it in program using EF Code First.
Here is link to my database: http://s11.postimg.org/6sv6cucgj/1462037_646961388683482_1557326399_n.jpg
And what I did:
public class GameModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreationTime { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string TotalTime { get; set; }
public DateTime RouteStartTime { get; set; }
public DateTime RouteEndTime { get; set; }
public int MaxPlayersPerTeam { get; set; }
public int CityId { get; set; }
public int CreatorId { get; set; }
[InverseProperty("Id")]
[ForeignKey("CreatorId")]
//public int TeamId { get; set; }
//[ForeignKey("TeamId")]
public virtual UserModel Creator { get; set; }
public virtual CityModel City { get; set; }
//public virtual TeamModel WinnerTeam { get; set; }
}
public class RegionModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<CityModel> Cities { get; set; }
}
public class CityModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int RegionId { get; set; }
public virtual RegionModel Region { get; set; }
public virtual ICollection<UserModel> Users { get; set; }
public virtual ICollection<GameModel> Games { get; set; }
}
public class UserModel
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public DateTime RegistrationDate { get; set; }
public string FacebookId { get; set; }
public int CityId { get; set; }
public virtual CityModel City { get; set; }
public virtual IEnumerable<GameModel> Games { get; set; }
}
For now I wanted to create 4 tables but I have some problems... I want to make CreatorId in GameModel, but it doesn't work... When i wrote UserId instead of CreatorId it was working ( without [InverseProperty("Id")] and [ForeignKey("CreatorId")]).
This is what i get:
The view 'The property 'Id' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type.' or its master was not found or no view engine supports the searched locations.
edit:
I changed it like this:
public int CityId { get; set; }
public int CreatorId { get; set; }
[ForeignKey("CityId")]
public virtual CityModel City { get; set; }
[ForeignKey("CreatorId")]
public virtual UserModel Creator { get; set; }
And there is another problem.
The view 'Introducing FOREIGN KEY constraint 'FK_dbo.UserModels_dbo.CityModels_CityId' on table 'UserModels' 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. See previous errors.' or its master was not found or no view engine supports the searched locations.
And I have no idea how to solve it.
The InversePropertyAttribute specifies, which navigation property should be used for that relation.
A navigation property must be of an entity type (the types declared in your model, GameModel for example) or some type implementing ICollection<T>, where T has to be an entity type. UserModel.Id is an int, which clearly doesn't satisfy that condition.
So, the inverse property of GameModel.Creator could be UserModel.Games if you changed the type to ICollection<GameModel>, or had to be left unspecified. If you don't specify an inverse property, EF will try to work everything out on its own (in this case it would properly recognize GameModel.Creator as a navigation property, but UserModel.Games would most likely throw an exception, as it is neither an entity type, nor does it implement ICollection<T> with T being an entity type, nor is it a primitive type from a database point of view). However, EF's work-everything-out-by-itself-magic doesn't cope too well with multiple relations between the same entity types, which is when the InversePropertyAttribute is needed.
A quick example that demonstrates the problem:
class SomePrettyImportantStuff {
[Key]
public int ID { get; set; }
public int OtherId1 { get; set; }
public int OtherId2 { get; set; }
[ForeignKey("OtherId1")]
public virtual OtherImportantStuff Nav1 { get; set; }
[ForeignKey("OtherId2")]
public virtual OtherImportantStuff Nav2 { get; set; }
}
class OtherImportantStuff {
[Key]
public int ID { get; set; }
public virtual ICollection<SomePrettyImportantStuff> SoldStuff { get; set; }
public virtual ICollection<SomePrettyImportantStuff> BoughtStuff { get; set; }
}
Here, EF knows that it has to generate 2 FKs from SomePrettyImportantStuff to OtherImportantStuff with the names Id1 and Id2, but it has no way to tell which of the IDs refers to the entity where it was sold from and which is the one it was bought from.
Edit: How to fix the cyclic reference problem
To fix that problem, your context class should override OnModelCreating and configure the foreign keys which shouldn't cascade on delete accordingly, like this:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<CityModel>().HasMany(c => c.Users).WithRequired(u => u.City)
.HasForeignKey(u => u.CityId).WillCascadeOnDelete(value: false);
// Add other non-cascading FK declarations here
base.OnModelCreating(builder);
}

Categories

Resources