IsInRole - Error Invalid column name 'UserId' - entity-framework

This is an MVC4 project using SimpleMembership with EntityFramework First approach.
I am getting 2 errors when I try to use User.IsInRole("Admin").
1. Invalid column name "UserId".
2. Invalid column name 'RoleId'.
I suspect the reason is because I have a user class and respective table defined where the primary key field is called "Id" instead of "UserId" (which is the default in MVC4 template). This project is near its completion so I dont want to revert to using UserId unless I really really have to.
public class User
{
public long Id { get; set; }
public string UserName { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Intro { get; set; }
public string Locale { get; set; }
}
The primary key columns on the other simple membership tables are the defaults as follows
Users : Id
webpages_Membership : UserId
webpages_UsersInRoles : Users_Id
webpages_Roles : RoleId
All other membership functionality works currently fine. ie. Login, Register, OAuth Login etc.
If anyone can shed any light on this please do as it killing me at the moment!
Thanks

Related

Invalid ColumnName, but ColumnName is correct (EFC)

So i'm trying to learn Design First EFC, but I have a problem getting Entities to work that have a many relationship with another Entity. I get this error message:SqlException: Invalid column name 'GuestListId'.
For experimentation I changed to the table into a one relationship. That works perfectly
This works:
public class GuestList
{
public Guid GuestListId { get; set; }
public Guest? Guest { get; set; }
}
However, when I want to have a many relationship, it does not work and fires the following errorcode: SqlException: Invalid column name 'GuestListId'.
This somehow does not work:
public class GuestList
{
public Guid GuestListId { get; set; }
public ICollection<Guest>? Guest { get; set; }
}
So I checked the GuestList table in SSMS, but that is identical to the entity in the sourcecode. I also updated the table but I still get the same problem. Does anyone have an idea what the problem is?
EDIT: So after a suggestion by someone, I added a GuestList property in Guest. But that still gives the same error:
public class Guest
{
public Guid GuestId { get; set; }
public string Name { get; set; }
public GuestList GuestList { get; set; }
}
So this is a picture of the sourcecode I have now that tries to insert data into the database.
Sourcecode
When I check the database, is see that GuestId value is not being assigned in the GuestList. But in the guest table I see that the GuestListid is being assigned:
Database Guest Table
Database GuestList Table
EDIT: I got it working now. As suggested, I migrated and updated my database. some tables have changed now. GuestList has only one column now and Guest has an extra column called GuestListId. See the pictures below:
enter image description here
enter image description here

EF6.1 Unique contraints over multiple columns

Consider the following class
public class Foo
{
[Key]
public int Id { get; set; }
[Required]
public virtual User User { get; set; }
[Required]
[MaxLength(20)]
public string Token { get; set; }
}
I need to combine User and Token into a unique constraint in the table. I thought this would be possible using the attribute [Index(IsUnique=true)]
I was testing this by first applying the attribute only to the Token property and then only to the User property. On the Token property I got the expected exception when adding duplicate records however on the User property I did not.
The user property is a foreign key (in this case an in) into the user table but this attribute wasn't enforcing uniqueness. Any idea why?
I stumbled across the answer through trial and error:
I needed to explicitly declare the foreign key for User rather than letting EF work it out. Then I can add the Index attribute to the foreign key property.
So rather than trying to do this:
[Required]
[Index("UserToken", 1, IsUnique = true)]
public virtual User User { get; set; }
I had to do this:
[Required]
[ForeignKey("UserId")]
public virtual User User { get; set; }
[Index("UserToken", 1, IsUnique = true)]
public int UserId { get; set; }

EntityFramework 6.1 Add Method

I have a very simple problem. Always use like this, but now not working, why i dont know.
I'm working on MVC 4 and Entity Framework 6.1.
I have sql table like picture below which name is Kategori,
Translation: KategoriID -> CategoryID, KategoriIsmi -> Category, UstKategoriId -> ParentCategoryID
KategoriID column has also, Primary Key and Identity Specification YES (Identity Increment 1, Seed 1)
And this is my Kategori Model class
public class Kategori
{
[Key]
public byte? KategoriID { get; set; }
[Required(ErrorMessage = "Please insert category name")]
public string KategoriIsmi { get; set; }
public byte? UstKategoriID { get; set; }
}
And my save code with EntityFramework
public void AddNewItem(Kategori item)
{
using (EmlakCMSContext _ent = new EmlakCMSContext())
{
_ent.Kategori.Add(item);
_ent.SaveChanges();
}
}
When I run this code
Income data (for save in db)
I have a error. And I write code, watch the error in IntelliTrace.
Error Translate: KategoriID alanı gereklidir -> CategoryID field is required.
But KategoriID field have set auto increment true.
How can i solve this problem? Thanks.
It's hard to tell as the error message is not in English. However, your primary key should not use a nullable data type. Change this:
public byte? KategoriID { get; set; }
To this:
public byte KategoriID { get; set; }
You may also need to tell entity framework that the column is an IDENTITY column:
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public byte KategoriID { get; set; }

How to configure EF 5.0 code first to create separate table with each enum type

I've used the following enum in my code first models
public enum Role
{
Reviewer = 1,
Requester = 2,
Approver = 3
}
It is used in the User entity like below
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Role Role { get; set; }
}
EF creates a single table Users in the database and role is stored there as int.
What I need here is another table named "Roles" with Id and Desc. Id is the value of the enum and Desc is its name. And this table will be automatically populated on model creation.
Does EF has some feature for it? Or do I have to create another entity for Role and populate that manually?

Invalid Column Name with EF

I modified the table UserProfile in the database with some extra columns and then modified the UserProfile class to reflect them:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
public string School { get; set; }
}
Obviously they are FirstName, Surname and School. For some reason though despite the register action saving details into all 3 of these new columns when I try to load the data via:
var context = new UsersContext();
var user = context.UserProfiles.First(n => n.UserName == model.UserName);
It says that School is an invalid ColumnName. I checked it was a string in both class and table so bit confused how to debug, help!
(Continued from comments on OP)
Rather than doing this manually, you should consider using the EF migrations framework - There are a number of benefits and it's more future-proof in case internal EF functionality changes.
See here for more information on migrations