Using enum as FK on EF 6 - entity-framework

I'd like to use an enum as Foreign Key in a Code-First app. Since enums are stored as int, I thought I could use the attribute [ForeignKey] on the enum property, but it throws this exception:
The types of all properties in the Dependent Role of a referential constraint
must be the same as the corresponding property types in the Principal Role
Here is an example of what I am trying to do:
public enum UserType
{
Administrator = 1,
Member = 2
}
public class User
{
public int UserId { get; set; }
public string Login { get; set; }
[ForeignKey("TypeDetails")]
public UserType Type { get; set;}
public virtual MasterType TypeDetails { get; set; }
}
public class MasterType
{
public int MasterTypeId { get; set; }
public string Description { get; set; }
...
}
Is it possible to do this or something similar through fluent api or migrations?
Thanks

Here's one I made earlier: https://www.nuget.org/packages/ef-enum-to-lookup
It's a nuget package that provides a method you can call in your Seed (initializer and/or migrations) which will automatically build lookup tables and add FKs where the enum is used. Usage info.
Enjoy :-) And let me know if it works for you (or anyone else for that matter!)

Related

.NET Core Entity Framework linking subtable to property

This is an existing .NET Core 3.1 project I inherited.
I have a class referring to a database table
public class SupportContract
{
public int Id { get; set; }
public DateTime StartDate { get; set; }
public int SupportContractStatusId { get; set; }
public virtual SupportContractStatus SupportContractStatus { get; set; }
}
and a sub table with a foreign key
public class SupportContractStatus
{
public int Id { get; set; }
public string SupportContractStatusName { get; set; }
}
This works fine I can get
supportContract.SupportContractStatus.SupportContractStatusName
But if I rename SupportContractStatusId to ContractStatusId in C# and the database, I get an error "SupportContractStatusId missing".
I cannot find any link between the column SupportContractStatusId and table SupportContractStatus anywhere in code nor is there any mention of the foreign key.
There is no link in the DbContext either.
Is this naming convention assumed by Entity Framework? How does the framework know of the foreign key?
Yes, the naming convention that EF expects by default is based on the class name, not the property name. It will look for ClassNameId or ClassName_Id. You can link the FK either through annotation or configuration.
I.e.
public int ContractStatusId { get; set; }
[ForeignKey("ContractStatusId")]
public virtual SupportContractStatus ContractStatus { get; set; }
Configuration is done through IEntityTypeConfiguration implementations or by implementing the OnModelCreating method in the DbContext and configuring the relationship within the modelBuilder. For occasional deviations from convention, the attribute approach can generally cover everything.

Entity Framework core 2.0 HasColumnType throw run time exception

I have a custom enum type EmployementState (complex type in EF 6 term, I think)
in OnModelCreating, the following code throw an run time exception.
modelBuilder.Entity<Employee>().Property(e => e.EmployementState.Value).HasColumnType("int");
The exception show below:
Message=The expression 'e => e.EmployementState.Value' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'.
cannot figure out how to get the syntax right or is there are something else I was missing?
Thank you for your help.
Assuming you have the following model for your EmployementState object that will hold the different states for your employee:
public class EmployementState
{
public int Id { get; set; }
public string Name { get; set; }
}
You can then add a reference like:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
// All your user properties here
public int EmployementStateId { get; set; }
public virtual EmployementState EmployementState { get; set; }
}
I recommend this because is the best approach when you are working with states describing your objects.
Of course being two separate models they are configurable so configurations like this one below are easy to implement.
modelBuilder.Entity<Employee>().Property(e => e.EmployementStateId)
.HasColumnName("employement_state_ID");

EF 5 Code First using Inheritence in the class

I am getting Error when trying to run this code.
Unable to determine the principal end of an association between the
types 'AddressBook.DAL.Models.User' and 'AddressBook.DAL.Models.User'.
The principal end of this association must be explicitly configured
using either the relationship fluent API or data annotations.
The objective is that i am creating baseClass that has commonfield for all the tables.
IF i don't use base class everything works fine.
namespace AddressBook.DAL.Models
{
public class BaseTable
{
[Required]
public DateTime DateCreated { get; set; }
[Required]
public DateTime DateLastUpdatedOn { get; set; }
[Required]
public virtual int CreatedByUserId { get; set; }
[ForeignKey("CreatedByUserId")]
public virtual User CreatedByUser { get; set; }
[Required]
public virtual int UpdatedByUserId { get; set; }
[ForeignKey("UpdatedByUserId")]
public virtual User UpdatedByUser { get; set; }
[Required]
public RowStatus RowStatus { get; set; }
}
public enum RowStatus
{
NewlyCreated,
Modified,
Deleted
}
}
namespace AddressBook.DAL.Models
{
public class User : BaseTable
{
[Key]
public int UserID { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Password { get; set; }
}
}
You need to provide mapping information to EF. The following article describes code-first strategies for different EF entity inheritance models (table-per-type, table-per-hierarchy, etc.). Not all the scenarios are directly what you are doing here, but pay attention to the mapping code because that's what you need to consider (and it's good info in case you want to use inheritance for other scenarios). Note that inheritance does have limitations and costs when it comes to ORMs, particularly with polymorphic associations (which makes the TPC scenario somewhat difficult to manage). http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
The other way EF can handle this kind of scenario is by aggregating a complex type into a "fake" compositional relationship. In other words, even though your audit fields are part of some transactional entity table, you can split them out into a common complex type which can be associated to any other entity that contains those same fields. The difference here is that you'd actually be encapsulting those fields into another type. So for example, if you moved your audit fields into an "Audit" complext type, you would have something like:
User.Audit.DateCreated
instead of
User.DateCreated
In any case, you still need to provide the appropriate mapping information.
This article here explains how to do this: http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx

How to map a list of component in Entity Framework 5?

I'm a NHibernate user and NHibernate allows me to create a very fine-grained model.
I'm porting an application from NHibernate to Entity Framework.
NHibernate allows me to define things like:
public class User : DomainEntity
{
public virtual Name Name { get; set; }
...
public virtual ICollection<LogonInformation> LogonInformations { get; set; }
}
public class Name
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class LogonInformation
{
public virtual string Ip { get; set; }
public virtual DateTime Date { get; set; }
}
Where Name and LogonInformation are mapped as < componentes >.
In special case,when NHibernate is creating the database, creates the UserId in LogonInformation table.
How can I do this using EntityFramework 5?
I've tried using the Complex Types but it does not seems to work since I still get the following exception:
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType
'LogonInformation' has no key defined. Define the key for this
EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet
'LogonInformations' is based on type 'LogonInformation' that has no
keys defined.
Your exception is complaining about LogonInformation not having a primary key. In order to establish a primary key you add the attribute Key to the property you want to be the primary key, for instance, if Ip is your primary key, your code would be:
public class LogonInformation
{
[Key]
public virtual string Ip { get; set; }
public virtual DateTime Date { get; set; }
}
UPDATE:
If you can't change LogonInformation you can set its primary key with Fluent-API (I don't like this way but it could solve your problem). In order to do that, you need to override the OnModelCreating method in your context like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<LogonInformation>().HasKey(logInfo => logInfo.Ip);
}

Entity Framework TPH Inheritance Data Modeling Issues

I'm new to Entity Framework and C#/.Net and trying to create a TPH inheritance model, I'm not sure if I should be or not, so if not, please advise,
Here's the model:
public abstract class Vote
{
public int VoteID { get; set; }
public int UserID { get; set; }
public bool Value { get; set; }
public DateTime DateCreated { get; set; }
public User User { get; set; }
}
public class ProjectVote_ : Vote
{
public int ProjectID { get; set; }
public virtual Project Project { get; set; }
}
public class CommentVote_ : Vote //There are three more like this, votes for different hings
{
public int CommentID { get; set; }
public virtual Comment Comment { get; set; }
}
Now the Project model (comment and model is similar)
public class Project
{
public int ProjectID { get; set; }
public string Title { get; set; }
public virtual ICollection<Vote> Vote { get; set; }
}
What happens is that ICollection creates a database column Project_ProjectID as the foreign key in the Vote table (I think) instead of using the ProjectID I defined. How do I fix it or should I model it differently. If the fluent API is the way to fix it, I don't know how to do that.
In the end I want to be able to use one table to store 5 different types of votes.
When you have related entities you don't need to have a property to store the FK in your model. Entity framework knows that it needs to make a FK to the Project table in ProjectVote when it detects Project in your ProjectVote_ model. Same thing with User and UserId and Comment and CommentId. You don't need to have a property that stores the FK in your model.
You are getting the FK column with the name you don't like "Project_ProjectID" because Entity framework is detecting that it needs to create a FK for your navigation property "Project". It's using it's own naming convention to create the column hence "Project_ProjectID".
If you want to provide your own name for the column override OnModelCreating in your DBContext class and add this fluent mapping.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>()
.HasMany(p => p.Vote)
.HasRequired(v => v.Project) //or .WithOptional(v => v.Project)
.Map(m => m.MapKey("ProjectId")); //or any other name you want.
}
And for the future this is a helpful reference for how to use the Fluent API. For example here is some documentation on how to custimize TPH with fluent.
Hope that helps!