I am trying to make and optional one to many relationship using fluent API. But it doesn't seem to work as I get this error:
InBuildingNavigator.Data.Models.ConnectionPointRoute_Segment: : Multiplicity conflicts with the referential constraint in Role 'ConnectionPointRoute_Segment_Target' in relationship 'ConnectionPointRoute_Segment'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
This is the modelcreation :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ConnectionPointRoute>()
.HasKey(c => new {c.ConnectionPointId, c.RouteId, c.SegmentId});
modelBuilder.Entity<ConnectionPoint>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.ConnectionPoint)
.HasForeignKey(c => c.ConnectionPointId);
modelBuilder.Entity<Route>()
.HasMany(c => c.ConnectionPointRoutes)
.WithRequired(x => x.Route)
.HasForeignKey(c => c.RouteId);
modelBuilder.Entity<Segment>()
.HasMany(c => c.ConnectionPointRoutes)
.WithOptional(s => s.Segment)
.HasForeignKey(s => s.SegmentId);
}
and this is the model :
public class ConnectionPointRoute
{
public int ConnectionPointId { get; set; }
public int RouteId { get; set; }
public int? SegmentId { get; set; }
public int Position { get; set; }
public ConnectionPoint ConnectionPoint { get; set; }
public Route Route { get; set; }
public Segment Segment { get; set; }
}
public class Segment
{
public Segment()
{
ConnectionPointRoutes = new List<ConnectionPointRoute>();
}
public int SegmentId { get; set; }
public int ConnectionPointIdEnd { get; set; }
public string ConnectionName { get; set; }
public string ConnectionInformation { get; set; }
public string Image { get; set; }
public string Direction { get; set; }
public ICollection<ConnectionPointRoute> ConnectionPointRoutes { get; set; }
}
Any thoughts?
It's because you are trying to define a composite primary key on ConnectionPointRoute that includes SegmentId which is nullable. You cannot define a primary key on a nullable column.
Related
I am using SQL and attempting to Add-Migration using Entity Framework Core. I am unsure how to resolve this. It is for associating a review system with the user and the product. This worked in SQLite. Now using SQL server. I have tried to provide everything while being brief. I can provide more if needed. Below is my code, can anyone please help?
An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Introducing FOREIGN KEY constraint 'FK_ProductReviews_AspNetUsers_ReviewerId' on table 'ProductReviews' 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 or index. See previous errors.
I have tried the commented out code.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<MemberReview>()
.HasKey(k => new { k.RevieweeId, k.ReviewerId });
builder.Entity<MemberReview>().
HasOne(u => u.Reviewer)
.WithMany(u => u.ReviewedMembers);
// .HasForeignKey(u => u.ReviewerId)
// .OnDelete(DeleteBehavior.Restrict);
builder.Entity<MemberReview>().
HasOne(u => u.Reviewee)
.WithMany(u => u.MemberReviews);
// .HasForeignKey(u => u.RevieweeId)
// .OnDelete(DeleteBehavior.Restrict);
builder.Entity<ProductReview>()
.HasKey(k => new { k.ReviewerId, k.ReviewedProductId });
builder.Entity<ProductReview>().
HasOne(u => u.ReviewedProduct)
.WithMany(u => u.ProductReviews);
//.HasForeignKey(u => u.ReviewedProductId)
//.OnDelete(DeleteBehavior.Restrict);
builder.Entity<ProductReview>().
HasOne(u => u.Reviewer)
.WithMany(u => u.ReviewedProducts);
//.HasForeignKey(u => u.ReviewerId)
//.OnDelete(DeleteBehavior.Restrict);
}
public class ProductReview
{
public Product ReviewedProduct { get; set; }
public User Reviewer { get; set; }
[Required]
public int ReviewerId { get; set; }
[Required]
[MaxLength(30)]
public string ReviewerUserName { get; set; }
[Required]
public int ReviewedProductId { get; set; }
[Required]
[MaxLength(35)]
public string Title { get; set; }
[Required]
[MaxLength(420)]
public string Review { get; set; }
[Required]
[MaxLength(2)]
public int Rating { get; set; }
}
public class User : IdentityUser<int>
{
[Required]
[MaxLength(12)]
public string UserType { get; set; }
[Required]
public DateTime DateOfEstablishment { get; set; }
[Required]
[MaxLength(75)]
public string KnownAs { get; set; }
public DateTime Created { get; set; }
public DateTime LastActive { get; set; }
[MaxLength(420)]
public string Description { get; set; }
public ICollection<Photo> Photos { get; set; }
public ICollection<Product> Products { get; set; }
// REVIEW THING
public ICollection<MemberReview> MemberReviews { get; set; }
public ICollection<MemberReview> ReviewedMembers { get; set; }
public ICollection<ProductReview> ReviewedProducts { get; set; }
// *****
}
public class Product
{
public int Id { get; set; }
[Required]
[MaxLength(75)]
public string Name { get; set; }
[Required]
[MaxLength(420)]
public string Description { get; set; }
public DateTime DateAdded { get; set; }
public User User { get; set; }
[Required]
public int UserId { get; set; }
// REVIEW THINGS
public ICollection<ProductReview> ProductReviews { get; set; }
// *****
}
I just tried this:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<MemberReview>()
.HasKey(e => new { e.RevieweeId, e.ReviewerId });
builder.Entity<ProductReview>()
.HasKey(e => new { e.ReviewerId, e.ReviewedProductId });
builder.Entity<MemberReview>()
.HasOne<User>(e => e.Reviewer)
.WithMany(e => e.MemberReviews)
.HasForeignKey(e => e.ReviewerId)
.OnDelete(DeleteBehavior.Restrict); ////////
//
builder.Entity<MemberReview>() //
.HasOne<User>(e => e.Reviewee) /// => only one of these two can be cascade
.WithMany(e => e.ReviewedMembers) //
.HasForeignKey(e => e.RevieweeId) //
.OnDelete(DeleteBehavior.Restrict); ////////
builder.Entity<ProductReview>()
.HasOne<User>(e => e.Reviewer)
.WithMany(e => e.ReviewedProducts)
.HasForeignKey(e => e.ReviewerId)
.OnDelete(DeleteBehavior.Restrict);
}
You had not provided the MemberReview class so I created this:
public class MemberReview
{
public User Reviewer { get; set; }
public int ReviewerId { get; set; }
public User Reviewee { get; set; }
public int RevieweeId { get; set; }
}
And this is the result:
I have two tables User and Message like below:
public partial class User
{
public int UserID { get; set; }
public string Name { get; set; }
public bool Status { get; set; }
public DateTime SubmitDate { get; set; }
public virtual ICollection<Message> Messages { get; set; }
}
public partial class Message
{
public int MessageID { get; set; }
public int SenderID { get; set; }
public int ReceiverID { get; set; }
public DateTime SubmitDate { get; set; }
public string Text { get; set; }
public bool Status { get; set; }
public int? ReplyToMessaageID { get; set; }
public virtual ICollection<Message> Messages { get; set; }
public virtual User SenderUser { get; set; }
public virtual User ReceiverUser { get; set; }
}
And in domain layer class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure domain classes using modelBuilder here
modelBuilder.Entity<Message>()
.HasOptional(c => c.Messages)
.WithMany()
.HasForeignKey(c => c.ReplyToMessaageID);
modelBuilder.Entity<Message>()
.HasRequired(c => c.SenderUser)
.WithMany(c => c.Messages)
.HasForeignKey(c => c.SenderID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Message>()
.HasRequired(c => c.ReceiverUser)
.WithMany(c => c.Messages)
.HasForeignKey(c => c.ReceiverID)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
Everything is fine but as can be seen in the final result, Message table has a waste column with name User_UserID.
How can I avoid creating it?
I'm trying to do one-one relationship for MVC5 codefirst. I've looked this page and did exactly same things but I've got an error.
Here is my classes and context:
Product:
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
/// <summary>
/// Display order
/// </summary>
public int Order { get; set; }
public string TitleBackgroundColor { get; set; }
public virtual TblClass TblClass { get; set; }
public virtual ICollection<Order> Orders { get; set; }
public virtual ICollection<Price> Prices { get; set; }
public virtual ICollection<ProductFeature> ProductFeatures { get; set; }
}
TblClass:
public class TblClass
{
[Key, ForeignKey("Product")]
public int ProductId { get; set; }
public string ClassName { get; set; }
public virtual ICollection<Permission> Permissions { get; set; }
public virtual Product Product { get; set; }
public int ClassOrder { get; set; }
}
DBContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Role>()
.HasMany<UserProfile>(r => r.UserProfiles)
.WithMany(u => u.Roles)
.Map(m =>
{
m.ToTable("webpages_UsersInRoles");
m.MapLeftKey("RoleId");
m.MapRightKey("UserId");
});
modelBuilder.Entity<TblClass>()
.HasKey(c => c.ProductId);
modelBuilder.Entity<Product>()
.HasOptional(f => f.TblClass)
.WithRequired(s => s.Product)
.Map(t => t.MapKey("ProductId"));
}
And when I try to run 'update-database -verbose' I've got this error:
The navigation property 'Product' declared on type 'YazililarGaranti.Domain.Entities.TblClass' has been configured with conflicting foreign keys.
You do not have to use .Map(t => t.MapKey("ProductId") when making an 1:1 relationship. This should work:
public class Product
{
public int ProductId { get; set; }
//... other properties
public virtual TblClass TblClass { get; set; }
//... other properties
}
public class TblClass
{
//[Key, ForeignKey("Product")] <-- remove these attributes
public int ProductId { get; set; }
public string ClassName { get; set; }
//.. other properties
public virtual Product Product { get; set; }
public int ClassOrder { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//other mappings
modelBuilder.Entity<TblClass>()
.HasKey(c => c.ProductId);
modelBuilder.Entity<Product>()
.HasKey(c => c.ProductId); //consider to use database generated option
//modelBuilder.Entity<Product>().Property(t => t.ProductId)
//.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Product>()
.HasOptional(f => f.TblClass)
.WithRequired(s => s.Product);
}
Hope this helps!
I'm using EF Code First to query and create a database. One of my entities (relationship) has two navigation properties to the same entity (activity). My problem is that if I use EF to create the database schema it will create four foreign key columns and constraints instead of two.
Here are the relevant code parts:
activity class:
public class Activity {
public virtual ICollection<Relationship> Successors { get; set; }
public virtual ICollection<Relationship> Predecessors { get; set; }
}
relationship class:
public class Relationship {
public virtual Activity Activity1 { get; set; }
public int Activity1_ID { get; set; }
public virtual Activity Activity2 { get; set; }
public int Activity2_ID { get; set; }
}
Relationship mapping class:
this.HasRequired(t => t.Activity1)
.WithMany(t => t.Predecessors)
.HasForeignKey(m => m.Activity1_ID)
.WillCascadeOnDelete(false);
this.HasRequired(t => t.Activity2)
.WithMany(t => t.Successors)
.HasForeignKey(m => m.Activity2_ID)
.WillCascadeOnDelete(false);
Database structure:
Is there a way to prevent the creation of the last two columns?
This should create you only 2 foreign key columns.
public class Activity
{
public int Id { set; get; }
public virtual ICollection<Relationship> Successors { get; set; }
public virtual ICollection<Relationship> Predecessors { get; set; }
}
public class Relationship
{
public int Id { set; get; }
public virtual Activity Activity1 { get; set; }
public int Activity1_ID { get; set; }
public virtual Activity Activity2 { get; set; }
public int Activity2_ID { get; set; }
}
And the DbContext class where i am specifying the relationship/FK nature on my OnModelCreating.
public class MyDb: DbContext
{
public MyDb():base("EfDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Relationship>()
.HasRequired(f => f.Activity1)
.WithMany(f => f.Predecessors)
.HasForeignKey(g => g.Activity1_ID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Relationship>().
HasRequired(f => f.Activity2)
.WithMany(f => f.Successors)
.HasForeignKey(g => g.Activity2_ID)
.WillCascadeOnDelete(false);
}
}
I don't understand why EF creates a nullable TemplateTask_Id column in my TemplateTaskDependancies table. I thought using a modelbuilder configuration class would solve the problem, but I must be missing something.
My domain classes are as follows.
[Table("TemplateTaskDependancies")]
public class TemplateTaskDependancy : Dependancy<TemplateTask>,
IDependancy<TemplateTask>
{
[Column("TaskId")]
public int TaskId { get; set; }
[Column("NeededTaskId")]
public int NeededTaskId { get; set; }
[ForeignKey("TaskId")]
public override TemplateTask Task { get; set; }
[ForeignKey("NeededTaskId")]
public override TemplateTask NeededTask { get; set; }
}
public abstract class Dependancy<T> : LoggedEntity
where T : LoggedEntity
{
[Column("TaskId")]
public int TaskId { get; set; }
[Column("NeededTaskId")]
public int NeededTaskId { get; set; }
[ForeignKey("TaskId")]
public abstract T Task { get; set; }
[ForeignKey("NeededTaskId")]
public abstract T NeededTask { get; set; }
}
public interface IDependancy<T> where T : LoggedEntity
{
int Id { get; set; }
int TaskId { get; set; }
int NeededTaskId { get; set; }
T NeededTask { get; set; }
T Task { get; set; }
State { get; set; }
}
public abstract class LoggedEntity : IObjectWithState
{
public int Id { get; set; } // primary key
// todo with Julie Lerman's repository pattern
}
In my context I have
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions
.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Configurations
.Add(new TemplateTaskDependancyConfiguration());
}
public class TemplateTaskDependancyConfiguration :
EntityTypeConfiguration<TemplateTaskDependancy>
{
public TemplateTaskDependancyConfiguration()
{
HasRequired(x => x.NeededTask)
.WithMany(y=>y.NeededTasks)
.HasForeignKey(z=>z.NeededTaskId)
.WillCascadeOnDelete(false);
HasRequired(x => x.NeededTask)
.WithMany(y => y.Dependancies)
.HasForeignKey(z => z.TaskId)
.WillCascadeOnDelete(false);
HasRequired(x=>x.Task)
.WithMany(y=>y.NeededTasks)
.HasForeignKey(z=>z.NeededTaskId)
.WillCascadeOnDelete(false);
HasRequired(x => x.Task)
.WithMany(y => y.Dependancies)
.HasForeignKey(z => z.TaskId)
.WillCascadeOnDelete(false);
}
}
Because you have no primary key defined anywhere?
By the way, it's dependEncy.
It turned out that the problem was caused by an unneeded collection of
public List<TemplateTaskDependancy> Tasks
inside my TemplateTask class.
i.e the foreign key table contained an extra collection of objects.