I have database first EF and I and create the model classes manually for Entity Framework - I didn’t generate any models.
I get this error :
ModelValidationException was unhandled HResult=-2146233088 Message=One or more validation errors were detected during model generation: UserToGroup EntityType 'UserToGroup' has no key defined. Define the key for this EntityType. UserToGroup EntityType EntitySet 'UserToGroup' is based on type 'UserToGroup' that has no keys defined.
Model classes:
public class Group
{
[key]
public int ID { get; set; }
public string Name { get; set; }
public int ClubId { get; set; }
}
public class User
{
[key]
public int ID { get; set; }
public string Name { get; set; }
public string email { get; set; }
}
public class UserToGroup
{
public int GroupID { get; set; }
public int UserID { get; set; }
}
In database script table UserToGroup
:
CREATE TABLE [dbo].[usertogroup]
(
[groupid] [INT] NOT NULL,
[userid] [INT] NOT NULL,
CONSTRAINT [IX_UserToGroup]
UNIQUE NONCLUSTERED ([groupid] ASC, [userid] ASC)
)
ON [PRIMARY]
GO
ALTER TABLE [dbo].[usertogroup] WITH CHECK
ADD CONSTRAINT [FK_UserToGroup_Group]
FOREIGN KEY([groupid]) REFERENCES [dbo].[group] ([id])
GO
ALTER TABLE [dbo].[usertogroup] CHECK CONSTRAINT [FK_UserToGroup_Group]
GO
ALTER TABLE [dbo].[usertogroup] WITH CHECK
ADD CONSTRAINT [FK_UserToGroup_User]
FOREIGN KEY([userid]) REFERENCES [dbo].[user] ([id])
GO
ALTER TABLE [dbo].[usertogroup] CHECK CONSTRAINT [FK_UserToGroup_User]
GO
My class context :
public class myContext : DbContextBase
{
public const string ConnectionStringName = @"xxxxxx";
public myContext() : base(ConnectionStringName)
{ }
public DbSet<Group> Group { get; set; }
public DbSet<User> User { get; set; }
public DbSet<User> UserToGroup { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
Thanks in advance for any help