MVC EF Code First one to one relationship error - asp.net

I want to have a list of stands (at a trade show) and a list of exhibitors.
The list of stands is separate to the list of exhibitors - however, once registered, I want the exhibitor to be able to book a stand.
When they select/book a stand - I would like to then be able to have a list the stands in my view, and also show the associated exhibitor who has booked it.
Likewise, I would like to list in another view, the exhibitors, and also which stand they have booked.
So I'm trying to setup a one to one relationship (using EF CodeFirst).
However, when trying to add a controller for either the Stand or the Exhibitor, I get the following error:
My models are:
public class Stand
{
public int StandID { get; set; }
public string Description { get; set; }
public bool Booked { get; set; }
public int ExhibitorID { get; set; }
public virtual Exhibitor Exhibitor { get; set; }
}
public class Exhibitor
{
public int ExhibitorID { get; set; }
public string Company { get; set; }
public int StandID { get; set; }
public virtual Stand Stand { get; set; }
}
I'm certain it's something to do with the "Virtual" part of the models.
Can anyone please help point out what should be updated, to allow the connection?
Thank you,
Mark

EF doesn't know which entity is the principal (parent) and which is the dependent (child). You need to declare a foreign key on the item that entity that should come first. You can do this with an annotation or a fluent mapping.
Annotation
Add the following namespace:
using System.ComponentModel.DataAnnotations.Schema;
Annotate your Stand class with the following annotation:
public class Stand
{
[ForeignKey("Exhibitor")]
public int StandID { get; set; }
public string Description { get; set; }
public bool Booked { get; set; }
public int ExhibitorID { get; set; }
public virtual Exhibitor Exhibitor { get; set; }
}
Fluent Mapping
Override your OnModelCreating method in your DbContext class to include:
modelBuilder.Entity<Stand>()
.HasOptional(s => s.Exhibitor)
.WithRequired(e => e.Stand);

The model you have created is not possible to work with relational databases. The Stand needs an ExibitorId while Exibitor need a StandId. The cyclic relationship does not allow you to insert any rows to either tables.
Assuming an Exibitor may have more than one Stand and converting the relationship to one-to-many is one option.
public class Stand
{
public int StandID { get; set; }
public string Description { get; set; }
public bool Booked { get; set; }
public int? ExhibitorID { get; set; }
public virtual Exhibitor Exhibitor { get; set; }
}
public class Exhibitor
{
public int ExhibitorID { get; set; }
public string Company { get; set; }
public virtual ICollection<Stand> Stands { get; set; }
}
Or you can use shared primary key mapping to make the relationship one-to-one. Where Stand is the principal entity. The Exibitor will use the StandID as its PK.
public class Stand
{
public int StandID { get; set; }
public string Description { get; set; }
public bool Booked { get; set; }
public virtual Exhibitor Exhibitor { get; set; }
}
public class Exhibitor
{
public int ExhibitorID { get; set; }
public string Company { get; set; }
public virtual Stand Stand { get; set; }
}
Using the Fluent API to configure the relationship.
modelBuilder.Entity<Exibitor>().HasRequired(e => e.Stand)
.WithOptional(s => s.Exibitor);

Related

Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths even after removing the affected field completely

I'm quite new to .net and entity framework (this is my first project) and I'm getting the following error when trying to update the database:
*Introducing FOREIGN KEY constraint 'FK_Rating_User_UserId' on table 'Rating' 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 tried doing what it says (at least I think so) by adding the following to my dbContext class:
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
modelbuilder.Entity<Rating>().HasOne(u => u.User).WithMany().OnDelete(DeleteBehavior.Restrict);
modelbuilder.Entity<Rating>().HasOne(g => g.Game).WithMany().OnDelete(DeleteBehavior.Restrict);
}
Not sure have I formulated that method correctly but it did not help (I tried with different DeleteBehavior like SetNull and NoAction)
The thing that really got me confused is that the issue appears even after removing all fields related to other tables from Rating class or even all references between all classes.
My Rating class:
public class Rating
{
public long RatingId { get; set; }
//[Rating]
public virtual Game Game { get; set; } // issue appears even after removing this and User line
//[Rating]
public int Score { get; set; }
public string CommentTitle { get; set; }
public string CommentDescription { get; set; }
//[Rating]
public virtual User User { get; set; }// issue appears even after removing this and Game line
}
User class:
public class User
{
public long UserId { get; set; }
//[Required]
public bool IsModerator { get; set; }
//[Required]
public string Username { get; set; }
//[Required]
public string Email { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
//[Required]
public string Password { get; set; }
//[Required]
public string Salt { get; set; }
public string Description { get; set; }
}
Game class:
public class Game
{
public long GameId { get; set; }
//[Required]
public virtual User User { get; set; }
//[Required]
public string Title { get; set; }
public string Description { get; set; }
//[Required]
public string PricingType { get; set; }
public float MinDonation { get; set; }
public float MaxDonation { get; set; }
//[Required]
public string FileLocation { get; set; }
public float AverageRaiting { get; set; }
public int DownloadCount { get; set; }
}
GameImage class (probably unrelated to the issue just wanted to give a full context)
public class GameImage
{
public long GameImageId { get; set; }
//[Required]
public virtual Game Game { get; set; }
//[Required]
public string Location { get; set; }
//[Required]
public bool IsThumbnail { get; set; }
}
dbContext class:
public class dbContext : DbContext
{
public dbContext(DbContextOptions<dbContext> options) : base(options)
{
}
public DbSet<User> User { get; set; }
public DbSet<Rating> Rating { get; set; }
public DbSet<GameImage> GameImage { get; set; }
public DbSet<Game> Game { get; set; }
}
The issue only appeared after I tried to update the database. The first few migrations and updates were ok, however, then I tried adding [Required] annotation (you can see them commented in the above code) as I noticed that most of the fields were created as nullable in my database - after that the issue starting to occur even after removing the annotations.
In case that matters, I'm using Visual Studio 2019 and SQL Server Express
Does anyone have any idea what may be the cause of this?
Edit:
Image of of my database schema diagram from SSMS
As you can see in the database schema it's visible that there are indeed cycles in the database, however, I cannot get rid of them as Entity Framework's command "Update-Database" does not update the DB and just throws the error mentioned above.
Based on my test, you can try the following steps to solve the problem.
First, please change your dbcontext class into the following code.
public class dbContext : DbContext
{
public dbContext() : base("name=MyContext") { }
public DbSet<User> User { get; set; }
public DbSet<Rating> Rating { get; set; }
public DbSet<GameImage> GameImage { get; set; }
public DbSet<Game> Game { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
Second, please delete all the tables the database.
Third, please try the following command in your package console.
PM> Update-Database -Force
Finally, you can see the new tables in the databse.

How can I set up two navigation properties of the same type in Entity Framework without use Fluent API

i'm trying create DB using codefirst. i want to create two ForeingKey from same table. But when i set up two navigation properties of the same type, get error like :
The foreign key name 'FollowedUser' was not found on the dependent type Models.UserUserWatchListItem'. The Name value should be a comma separated list of foreign key property names.
public class UserUserWatchListItem
{
public int Id { get; set; }
[Key,ForeignKey("FollowedUser")]
public virtual User FollowedUser { get; set; }
public int FollowedUserId { get; set; }
[Key,ForeignKey("FolloweeUser")]
public int FolloweeUserId { get; set; }
public virtual User FolloweeUser { get; set; }
}
Use this :
public class UserUserWatchListItem
{
public int Id { get; set; }
public int FollowedUserId { get; set; }
public int FolloweeUserId { get; set; }
[ForeignKey("FollowedUser")]
[InverseProperty("FollowedUsers")]
public virtual User FollowedUser { get; set; }
[ForeignKey("FolloweeUser")]
[InverseProperty("FolloweeUsers")]
public virtual User FolloweeUser { get; set; }
}
public class User
{
...
[InverseProperty("FollowedUser")]
public virtual ICollection<UserUserWatchListItem> FollowedUsers { get; set; }
[InverseProperty("FolloweeUser")]
public virtual ICollection<UserUserWatchListItem> FolloweeUsers { get; set; }
}

AutoMpper + Map Complex Nested Many to Many Relationship

I have domain model like this
public class EntityOne
{
public int EnityOneId { get; set; }
public int EntityOnePropertyOne { get; set; }
public List<EntityTwo> EntityTwos { get; set; }
}
public class EntityTwo
{
public int EntityTwoId { get; set; }
public string EntityTwoPropertyOne { get; set; }
public int EntityThreeId { get; set; }
public int EnityOneId { get; set; }
public virtual EntityOne EntityOne { get; set; }
public virtual EntityThree EntityThree { get; set; }
}
public class EntityThree
{
public int EntityThreeId { get; set; }
public string EntityThreePropertyOne { get; set; }
}
and I have DTO like this
public class EntityDTO
{
public int EntityOnePropertyOne { get; set; }
public string EntityThreePropertyOne_ValueOne { get; set; }
public string EntityThreePropertyOne_ValueTwo { get; set; }
public string EntityThreePropertyOne_ValueThree { get; set; }
public string EntityThreePropertyOne_ValueFour { get; set; }
public string EntityThreePropertyOne_ValueFive { get; set; }
}
I want to configure mapping from DTO to DomainModel and the reverse using AutoMapper but I didnt know how to do that... any suggestion or help
I'm not sure what you're trying to accomplish here.
I get that you want to map to EntityDTO, but from what other type? I will assume you want to use EntityTwo as the source.
In that case,
EntityOnePropertyOne: Will be obtained automatically via Flattening from the source (EntityTwo) - So, no problem here.
EntityThreePropertyOne_ValueOne: This will assume you have a property called EntityThree (which you do), and within that type, a property called PropertyOne_ValueOne of type int (which you don't). Same applies for the rest.
The other way around will get trickier, since I see there will be lots of properties ignored, so you need to tell AutoMapper, that you don't want it to be concerned about all that bunch of properties in your complex type, that don't come from the DTO.

EF codefirst relationships

Could someone show me how to create a relationship in my EF codefirst example - I want a relationship on the Products class that has a many relationship to the Product_Spec class so when I compile the code it will have relationships when the database is generated, and also a relationship for the Specification class related to the Product_Spec
Data Context class
classes:
namespace MvcApplication1.Models
{
public class Department
{
[Key]
public long Id { get; set; }
[Required(ErrorMessage="Please enter a name for the departments.")]
[DataType(DataType.Text)]
public string Name { get; set; }
[DataType(DataType.Text)]
[Required(ErrorMessage = "Please enter a valid url for the department.")]
public string Url { get; set; }
public virtual List<Product> Products { get; set; }
}
public class Product
{
[Key]
public long Id { get; set; }
[ForeignKey("FK_Department_Id")]
public long DepartmentId { get; set; }
[DataType(DataType.Text)]
public string Title { get; set; }
[DataType(DataType.Currency)]
public decimal UnitPrice { get; set; }
[DataType(DataType.Currency)]
public decimal SellPrice { get; set; }
}
public class Product_Spec
{
[ForeignKey("FK_Spec_ProductId")]
public long ProductId { get; set; }
[ForeignKey("FK_Spec_SpecId")]
public long SpecId { get; set; }
}
public class Specification
{
[Key]
public long SpecId { get; set; }
[DataType(DataType.Text)]
[Required(ErrorMessage = "Please enter a product specification type.")]
public string Type { get; set; }
[DataType(DataType.Text)]
[Required(ErrorMessage = "Please enter a product specification value.")]
public string Value { get; set; }
}
}
namespace MvcApplication1
{
public class DataContext : DbContext
{
public DbSet<Department> Department { get; set; }
public DbSet<Product> Product { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>().HasRequired(x => x.Products)
.WithMany().HasForeignKey(x => x.Id).WillCascadeOnDelete(true);
modelBuilder.Entity<Product>().HasOptional(x => x.Product_Specs)
.WithMany().HasForeignKey(x =>x.ProductId) // this lines doesn't work
base.OnModelCreating(modelBuilder);
}
}
}
I think you should set column names in ForeignKey attribute, not constraint names:
public class Product
{
[Key]
public long Id { get; set; }
public long DepartmentId { get; set; }
[DataType(DataType.Text)]
public string Title { get; set; }
[DataType(DataType.Currency)]
public decimal UnitPrice { get; set; }
[DataType(DataType.Currency)]
public decimal SellPrice { get; set; }
[ForeignKey("DepartmentId")]
public virtual Department Department { get; set; }
public ICollection<Product_Spec> ProductSpecs { get; set; }
}
public class Product_Spec
{
public long ProductId { get; set; }
public long SpecId { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product {get; set;}
}
It looks like you're trying to create a Many-Many relationship between Products and Specifications. If that's the case, you don't need to define Product_Spec, using the default conventions, Entity Framework will create your required junction table for you provided you make some alterations to your entities (to define the relationship).
In your case, you could make the following alterations:
public class Product
{
// Your other code
// [ForeignKey("FK_Department_Id")] - Not required, EF will configure the key using conventions
public long DepartmentId { get; set; }
public virtual ICollection<Specification> Specifications { get; set; } // Navigation property for one end for your Product *..* Specification relationship.
}
public class Specification
{
// Your other code
public virtual ICollection<Product> Products { get; set; }
}
When your tables are created, you should see a table with a name like SpecificationProducts, which is the junction table used to hold your many..many Product/Specification relationship.
If you needed to explicitly define this mapping (for example if you had an existing tables), you should be able to do something like this:
modelBuilder.Entity<Product>().
HasMany(s => s.Specifications).
WithMany(p => p.Products).
Map(
m =>
{
m.MapLeftKey("ProductId");
m.MapRightKey("SpecId");
m.ToTable("SpecificationProducts");
});

multiple relationships with same table by Dataannotation with code first

I have the following model:
public class Member
{
public int Id { get; set; }
public string Username { get; set; }
public int MainEmailId { get; set; }
public virtual Email MainEmail { get; set; }
public virtual ICollection<Email> MemberEmails { get; set; }
}
public partial class Email
{
public Int32 Id { get; set; }
public String Email { get; set; }
public int MemberId { get; set; }
public virtual Member Member { get; set; }
}
As you can see I wish to create a:
one-to-one relation from the Member to MemberEmail (the main email address)
one-to-many relation from Member to MemberEmail
I know how to do this with Code First Fluent API. However I need to do it with DataAnnotations only. Is this possible?
Thanks a lot.

Resources