I'm developing a Web Api using ASP.NET MVC5 and I have successfully implemented a Custom IdentityUser.Like So
public partial class Authentication : IdentityUser
{
public Authentication()
{
this.LoginSessions = new List<LoginSession>();
}
[Required]
public string PersonId { get; set; }
public virtual Lecturer Lecturer { get; set; }
public virtual Student Student { get; set; }
public virtual ICollection<LoginSession> LoginSessions { get; set; }
}
with the following Maps
public class IdentityUserLoginConfiguration: EntityTypeConfiguration<IdentityUserLogin>
{
public IdentityUserLoginConfiguration()
{
HasKey(t => t.UserId);
}
}
public class IdentityRoleConfiguration : EntityTypeConfiguration<IdentityRole>
{
public IdentityRoleConfiguration()
{
HasKey(t => t.Id);
}
}
public class AuthenticationMap : EntityTypeConfiguration<Authentication>
{
public AuthenticationMap()
{
// Primary Key
this.HasKey(t => t.PersonId);
// Properties
this.Property(t => t.PersonId)
.IsRequired()
.HasMaxLength(14);
// Table & Column Mappings
this.ToTable("Authentication");
this.Property(t => t.PersonId).HasColumnName("PersonID");
// Relationships
this.HasOptional(t => t.Lecturer)
.WithOptionalPrincipal(t => t.Authentication);
this.HasOptional(t => t.Student)
.WithOptionalPrincipal(t => t.Authentication);
}
}
But now my problem is that when I'm adding a user to a Role Using the
UserManager.AddToRole()
method I get a DBValidationError
This is really worrying me and would like some help as soon as possible
Related
Hi I have a problem with relationship between my tables. When i try to get values from table i have an error:
OrderDetails_Order_Target_OrderDetails_Order_Source: : The number of
properties in the Dependent and Principal Roles in a relationship
constraint must be identical. OrderDetails_Processor_Source: :
Multiplicity is not valid in Role 'OrderDetails_Processor_Source' in
relationship 'OrderDetails_Processor'. Because the Dependent Role
refers to the key properties, the upper bound of the multiplicity of
the Dependent Role must be '1'.
OrderDetails_Processor_Target_OrderDetails_Processor_Source: : The
number of properties in the Dependent and Principal Roles in a
relationship constraint must be identical.
My classes and relationship look that:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>().HasKey(x => x.ID_User);
modelBuilder.Entity<Processor>().HasKey(x => x.Product_ID);
modelBuilder.Entity<GPU>().HasKey(x => x.Product_ID);
modelBuilder.Entity<Orders>().HasKey(x => new { x.Order_ID, x.User_ID });
modelBuilder.Entity<OrderDetails>().HasKey(x => new { x.Order_ID, x.Product_ID });
modelBuilder.Entity<OrderDetails>().HasRequired(x => x.Processor).WithMany(x => x.OrderDetails).HasForeignKey(x => new { x.Product_ID, x.Order_ID });
modelBuilder.Entity<OrderDetails>().HasRequired(x => x.GPU).WithMany(x => x.OrderDetails);
modelBuilder.Entity<OrderDetails>().HasRequired(x => x.Order).WithMany(x => x.OrderDetails);
modelBuilder.Entity<Orders>().HasMany(x => x.OrderDetails).WithRequired(x => x.Order);
modelBuilder.Entity<Orders>().HasRequired(x => x.User).WithMany(x => x.Orders);
modelBuilder.Entity<Processor>().HasMany(x => x.OrderDetails).WithRequired(x => x.Processor).HasForeignKey(x => new { x.Product_ID, x.Order_ID });
modelBuilder.Entity<GPU>().HasMany(x => x.OrderDetails).WithRequired(x => x.GPU);
modelBuilder.Entity<Users>().HasMany(x => x.Orders).WithRequired(x => x.User);
}
public class OrderDetails
{
public int Order_ID { get; set; }
public int Product_ID { get; set; }
public int Quantity { get; set; }
[ForeignKey(nameof(Product_ID))]
public Processor Processor { get; set; }
[ForeignKey(nameof(Product_ID))]
public GPU GPU { get; set; }
[ForeignKey(nameof(Order_ID))]
public Orders Order { get; set; }
}
public class Processor
{
[Key]
public int Product_ID { get; set; }
public string Title { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public ICollection<OrderDetails> OrderDetails { get; set; }
}
And here i get an error
public class EfProcessorContext : IProcessorRepository
{
private EfDbContext context = new EfDbContext();
public IEnumerable<Processor> Processors
{
get
{
return context.Processors.Include(x => x.OrderDetails);
}
}
}
What wrong I do?
The reason may be for the error is incorrectly configured relations in your model.
How to fix: The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical?
https://entityframework.net/knowledge-base/28699205/-the-number-of-properties-in-the-dependent-and-principal-roles-in-a-relationship-constraint-must-be-identical--issue-in-entity-framework
"The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical" issue in entity framework
i cannot create database with ef core
error : Introducing FOREIGN KEY constraint 'FK_Product_User_UserId' on table 'Product' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
this is my product class
{
public Product()
{
}
public Guid UserId { get; set; }
public Guid CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string PhotoPath { get; set; }
public decimal Price { get; set; }
public Category Category { get; set; }
public User User { get; set; }
}
and
this is my user class
{
public User()
{
Payments = new HashSet<Payment>();
Categories = new HashSet<Category>();
Products = new HashSet<Product>();
}
public string Username { get; set; }
public Guid Password { get; set; }
public ICollection<Payment> Payments { get; set; }
public ICollection<Category> Categories { get; set; }
public ICollection<Product> Products { get; set; }
}
its mapping class
{
public ProductMap()
{
}
public void Configure(EntityTypeBuilder<Product> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.UserId).IsRequired();
builder.Property(x => x.CreatedDate).IsRequired();
builder.Property(x => x.Description).HasMaxLength(500);
builder.Property(x => x.IsActive).IsRequired();
builder.Property(x => x.Name).HasMaxLength(500).IsRequired();
builder.Property(x => x.PhotoPath).HasMaxLength(4000);
builder.Property(x => x.Price).HasColumnType("decimal(10,3)").IsRequired();
builder.HasOne(x => x.Category).WithMany(x => x.Products).HasForeignKey(x => x.CategoryId);
builder.HasOne(x => x.User).WithMany(x => x.Products).HasForeignKey(x => x.UserId);
}
}
and i cannot create database cause error like this posts title.
what can i do?
Thanks.
You are saying that each user has many Categories and each Category has many Products so you have to remove this line from the User class since it causing cycle path
public ICollection<Product> Products { get; set; }
And also fix the last line of the Configure method:
builder.HasOne(x => x.User).WithMany(x => x.Categories).HasForeignKey(x => x.UserId);
I am using C#.net and ef core. I have the models below. When I get my list of competitions, I want to only get my related user. However, I am getting the User and all the User's competitions. How can I achieve this? I had to do the following to get my list of competitions to show:
.AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
public partial class Competition
{
public int CompetitionId { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
public partial class User
{
public int UserId { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public ICollection<Competition> Competitions { get; set; }
}
I have an api that uses entity framework to make calls to my database using the models describe above. The call in my api that causes the circular reference is the following:
[Produces("application/json")]
[Route("api/Competitions")]
public class CompetitionsController : Controller
{
private readonly ApplicationDBContext _context;
public CompetitionsController(ApplicationDBContext context)
{
_context = context;
}
// GET: api/Competitions
[HttpGet]
public IEnumerable<Competition> GetCompetitions()
{
//return _context.Competitions;
return _context.Competitions
.Include(u => u.User).ToList();
}
}
Below is my onmodelcreating code block in my ApplicationDBContext class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Competition>(entity =>
{
entity.HasKey(e => e.CompetitionId);
entity.HasOne(d => d.User)
.WithMany(p => p.Competitions)
.HasForeignKey(d => d.UserId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Competitions_Users");
});
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(e => e.UserId);
entity.HasIndex(e => e.UserName)
.HasName("UC_UserName")
.IsUnique();
entity.Property(e => e.Email)
.HasMaxLength(40)
.IsUnicode(false);
entity.Property(e => e.UserName)
.HasMaxLength(40)
.IsUnicode(false);
});
}
Old question, but in case someone still looking.
One solution is:
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
You should have ignored Competitions collection in user model while building.
modelBuilder.Entity<User>()
.Ignore(b => b.Competitions);
I searched on site for solving my problem, but I still haven't solved it.
I have 2 entities:
public class Article
{
[Key]
public Guid ID { get; set; }
public Guid? ApprovedBy_ID { get; set; }
public Guid CreatedBy_ID { get; set; }
public virtual Profile ApprovedBy { get; set; }
public virtual Profile CreatedBy { get; set; }
//New guid for new article
public Article()
{
ID = Guid.NewGuid();
}
}
public class Profile
{
[Key]
public Guid ID { get; set; }
[Required]
[StringLength(100)]
public string FullName { get; set; }
public Profile()
{
ID = Guid.NewGuid();
}
}
And here is my insert-logic:
private readonly iContext context;
public ArticleLogic()
{
context = new iContext();
}
public IEnumerable<Article> GetAllArticle()
{
return context.Articles.Include("Categories").Include("Pictures").Include("ApprovedBy").Include("CreatedBy").Include("Template");
}
public Article AddArticle(Article article)
{
try
{
Profile pf = context.Profiles.First();
context.Profiles.Attach(pf);
Article art = new Article();
art.Title = article.Title;
art.Description = article.Description;
art.Content = article.Content;
art.Tag = article.Tag;
art.Template = article.Template;
//pf has ID = '0816f19c-31c1-4103-8f51-ba422beab1c0' (first row in database)
art.CreatedBy = pf;
art.CreatedBy_ID = pf.ID;
context.Articles.Add(art);
context.SaveChanges();
return article;
}
catch (Exception ex)
{
//But ex throw error duplicate ID = '7aa1d064-54ff-47b9-807d-db422fa71f8c' (second row in database)
Debug.WriteLine(ex.StackTrace);
throw ex;
}
}
DB Context:
public myContext()
: base("name=abc")
{
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = false;
}
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Role> Roles { get; set; }
public virtual DbSet<Template> Templates { get; set; }
public virtual DbSet<Article> Articles { get; set; }
public virtual DbSet<Picture> Pictures { get; set; }
public virtual DbSet<Profile> Profiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//One-to-many: Article - Picture (nullable)
modelBuilder.Entity<Picture>()
.HasOptional<Article>(c => c.Article)
.WithMany(p => p.Pictures)
.HasForeignKey(f => f.ArticleId);
////One-to-many: Role - Profile
modelBuilder.Entity<Profile>()
.HasRequired<Role>(i => i.Role)
.WithMany(i => i.Profiles)
.HasForeignKey(f => f.RoleID);
///Many-to-many: Article - Category
modelBuilder.Entity<Article>()
.HasMany(t => t.Categories)
.WithMany(t => t.Articles)
.Map(m => {
m.ToTable("ArticleCategories");
m.MapLeftKey("ArticleId");
m.MapRightKey("CategoryId");
});
//
modelBuilder.Entity<Article>()
.HasOptional(a => a.ApprovedBy)
.WithMany(a => a.ArticleApprovedBy)
.HasForeignKey(f=>f.ApprovedBy_ID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Article>()
.HasRequired(a => a.CreatedBy)
.WithMany(a => a.ArticleCreatedBy)
.HasForeignKey(f => f.CreatedBy_ID)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
I am using CodeFirst EF 6.1.3 and I am getting an error: Violation of PRIMARY KEY constraint 'PK_Profiles'. Cannot insert duplicate key in object 'dbo.Profiles' like this link Entity Framework Code First - Cannot insert duplicate key in object 'dbo.T_CRProviders'?
Can anyone help me? Thanks.
context thinks pf is new and should be inserted into the DB. Show the instantiation and configuration of context - do you have change tracking disabled?
You need to tell EF that a Profile can be referenced multiple times by Article:
modelBuilder.Entity<Article>()
.HasOptional(a=>a.ApprovedBy)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Article>()
.HasOptional(a=>a.CreatedBy)
.WithMany()
.WillCascadeOnDelete(false);
The HasOptional could also be HasRequired if the property is mandatory (not null in the DB).
Adding WithMany is a key here if you want (as you do in your sample) be able to associate a profile to multiple properties of Article (and multiple Articles too).
PS: I wrote this code by memory, so something could need adjustment.
Your line:
context.Profiles.Attach(pf);
will attach the pf, however it is already tracked by EF. You just got it from the database. The pf will be reinserted by EF, and this fails. Just remove the line and you will be fine.
Add/Attach and Entity States
Im getting an error "Invalid column name 'FeeLevel_LevelId' which makes absolutely no sense considering all properties are simple types and there is no FeeLevel nor a LevelId object in this object.
So my context is:
public partial class FeesDbContext : DisconnectedEntityContext
{
public DbSet<Currency> Currencies { get; set; }
public DbSet<FeeLevel> FeeLevels { get; set; }
public DbSet<FeeLevelDetail> FeeLevelDetails { get; set; }
public DbSet<FeeType> FeeTypes { get; set; }
public DbSet<MemberFeeDiscountLevel> MemberFeeDiscountLevels { get; set; }
public FeesDbContext()
: base("FeesDb") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new FeeLevelMap());
modelBuilder.Configurations.Add(new FeeLevelDetailMap());
modelBuilder.Configurations.Add(new FeeTypeMap());
modelBuilder.Configurations.Add(new CurrencyMap());
modelBuilder.Configurations.Add(new MemberFeeDiscountLevelMap());
}
public static void ApplyChanges<TEntity>(TEntity root)
where TEntity : class, IObjectWithState
{
using (var context = new FeesDbContext())
{
context.Set<TEntity>().Add(root);
foreach (var entry in context.ChangeTracker.Entries<IObjectWithState>())
{
IObjectWithState stateInfo = entry.Entity;
entry.State = ConvertState(stateInfo.State);
}
context.SaveChanges();
}
}
}
The POCO is:
public partial class MemberFeeDiscountLevel : AbstractState
{
public long MemberFeeDiscountLevelId { get; set; }
public System.Guid MemberId { get; set; }
public short MemberAsType { get; set; }
public long FeeDiscountLevelId { get; set; }
public System.DateTime FeeDiscountLevelAppliedDate { get; set; }
public Nullable<System.DateTime> FeeDiscountLevelExpiresDate { get; set; }
public Nullable<long> FallbackFeeDiscountLevelId { get; set; }
public System.Guid UserId { get; set; }
public System.DateTime LastModified { get; set; }
public MemberFeeDiscountLevel(ObjectState state) : base(state) { }
public MemberFeeDiscountLevel()
{
}
}
It's mapping is:
public class MemberFeeDiscountLevelMap : EntityTypeConfiguration<MemberFeeDiscountLevel>
{
public MemberFeeDiscountLevelMap()
{
// Primary Key
this.HasKey(t => t.MemberFeeDiscountLevelId);
this.Ignore(t => t.State);
// Properties
// Table & Column Mappings
this.ToTable("MemberFeeDiscountLevel");
this.Property(t => t.MemberFeeDiscountLevelId).HasColumnName("MemberFeeDiscountLevelId");
this.Property(t => t.MemberId).HasColumnName("MemberId");
this.Property(t => t.MemberAsType).HasColumnName("MemberAsType");
this.Property(t => t.FeeDiscountLevelId).HasColumnName("FeeDiscountLevelId");
this.Property(t => t.FeeDiscountLevelAppliedDate).HasColumnName("FeeDiscountLevelAppliedDate");
this.Property(t => t.FeeDiscountLevelExpiresDate).HasColumnName("FeeDiscountLevelExpiresDate");
this.Property(t => t.FallbackFeeDiscountLevelId).HasColumnName("FallbackFeeDiscountLevelId");
this.Property(t => t.UserId).HasColumnName("UserId");
this.Property(t => t.LastModified).HasColumnName("LastModified");
}
}
The database table is:
and it has not relationships. Yet EF is generating the following SQL:
exec sp_executesql N'INSERT [dbo].[MemberFeeDiscountLevel]([MemberId], [MemberAsType], [FeeDiscountLevelId], [FeeDiscountLevelAppliedDate], [FeeDiscountLevelExpiresDate], [FallbackFeeDiscountLevelId], [UserId], [LastModified], [FeeLevel_LevelId])
VALUES (#0, #1, #2, #3, #4, #5, #6, #7, #8, NULL)
SELECT [MemberFeeDiscountLevelId]
FROM [dbo].[MemberFeeDiscountLevel]
WHERE ##ROWCOUNT > 0 AND [MemberFeeDiscountLevelId] = scope_identity()',N'#0 uniqueidentifier,#1 smallint,#2 bigint,#3 datetime2(7),#4 datetime2(7),#5 bigint,#6 uniqueidentifier,#7 datetime2(7),#8 int',#0='DAF771D1-079F-4743-B5C7-FD0FA1C63E19',#1=0,#2=1012,#3='2014-01-24 12:05:36.0608347',#4='2014-02-01 00:00:00',#5=1018,#6='EEDF2C83-2123-4B1C-BF8D-BE2D2FA26D09',#7='2014-01-24 12:05:36.0608347'
go
UPDATE:
Creating a new Fees2DbContext stripping out the other DbSets "fixes" the problem.... but I dont know why... none of these classes / sets are related to the class in question.
public partial class Fees2DbContext : DisconnectedEntityContext
{
public DbSet<MemberFeeDiscountLevel> MemberFeeDiscountLevels { get; set; }
public Fees2DbContext()
: base("FeesDb") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MemberFeeDiscountLevelMap());
}
public static void ApplyChanges<TEntity>(TEntity root)
where TEntity : class, IObjectWithState
{
using (var context = new Fees2DbContext())
{
context.Set<TEntity>().Add(root);
foreach (var entry in context.ChangeTracker.Entries<IObjectWithState>())
{
IObjectWithState stateInfo = entry.Entity;
entry.State = ConvertState(stateInfo.State);
}
context.SaveChanges();
}
}
}
UPDATE 2:
public partial class FeeLevel : AbstractState
{
public FeeLevel()
{
this.FeeLevelDetails = new List<FeeLevelDetail>();
this.MemberFeeDiscountLevels = new List<MemberFeeDiscountLevel>();
}
public long LevelId { get; set; }
public string LevelName { get; set; }
public int CurrencyId { get; set; }
public System.DateTime LastModified { get; set; }
public bool IsSystemOwned { get; set; }
public System.Guid UserId { get; set; }
public virtual Currency Currency { get; set; }
[ScriptIgnore]
public virtual ICollection<FeeLevelDetail> FeeLevelDetails { get; set; }
public virtual ICollection<MemberFeeDiscountLevel> MemberFeeDiscountLevels { get; set; }
}
public class FeeLevelMap : EntityTypeConfiguration<FeeLevel>
{
public FeeLevelMap()
{
// Primary Key
this.HasKey(t => t.LevelId);
this.Ignore(t => t.State);
// Properties
this.Property(t => t.LevelId);
// .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.LevelName)
.IsRequired()
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("FeeLevel");
this.Property(t => t.LevelId).HasColumnName("LevelId");
this.Property(t => t.LevelName).HasColumnName("LevelName");
this.Property(t => t.CurrencyId).HasColumnName("CurrencyId");
this.Property(t => t.LastModified).HasColumnName("LastModified");
this.Property(t => t.UserId).HasColumnName("UserId");
// Relationships
this.HasRequired(t => t.Currency)
.WithMany(t => t.FeeLevels)
.HasForeignKey(d => d.CurrencyId);
}
}
FeeLevel.MemberFeeDiscountLevels is a navigation property and it introduces a one-to-many relationship between FeeLevel and MemberFeeDiscountLevel: A FeeLevel can have many MemberFeeDiscountLevels which means at the same time that a MemberFeeDiscountLevel has a single FeeLevel. Although you don't have a navigation and foreign key property in MemberFeeDiscountLevel the database must have a foreign key in the MemberFeeDiscountLevel table in order to model this relationship. EF assumes a default FK name as "related entity name+underscore+primary key name" = FeeLevel_LevelId. Because your database table doesn't have this column you get the exception.