EF.core beginner here.
In my app I have Persons. These are connected to other persons (in that same table), and those connections have a 'level'. I've added a join table called connection to store who is connected to who and on what level. When trying to do an Add-migration i get the following error:
Unable to determine the relationship represented by navigation property 'Connection.Person' of type 'Person'. Either manually configure the relationship, or ignore this property from the model.
There is no problem on my other join tables that reference two different tables. What am i missing here?
I'm using EF.core 1.1, with .net 4.5, code below
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string NickName { get; set; }
public List<Connection> Connections { get; set; }
}
public class Connection
{
public int PersonId { get; set; }
public Person Person { get; set; }
public int SecondPersonId { get; set; }
public Person SecondPerson { get; set; }
public int ConnectionLevelId { get; set; } // connection Level
public ConnectionLevel Level { get; set; }
}
public class ConnectionLevel
{
public int Id { get; set; }
public string Name { get; set; }
//rights
}
public class testContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Connection> Connections { get; set; }
public DbSet<ConnectionLevel> ConnectionLevels { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=master;Trusted_Connection=True;");
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Connection>().HasKey(s => new { s.PersonId, s.SecondPersonId });
base.OnModelCreating(modelBuilder);
}
}
Related
I'm creating a CRUD project in asp.net entity framework, and from my understanding, in order to see the data on the HTML page I have to scaffold the pages. For example I have two entity models Student and Major
This is the Student entity
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("Major")]
public int? MajorId { get; set; }
public Major Major { get; set; }
[ForeignKey("Advisor")]
public int AdvisorId { get; set; }
public Advisor Advisor { get; set; }
public ICollection<StudentCourses>? StudentCourses { get; set; }
}
This is the Major entity
public class Major
{
public int MajorId { get; set; }
public string MajorName { get; set; }
public ICollection<Student> Students { get; set; }
public ICollection<Advisor> Advisors { get; set; }
public ICollection<MajorCourse> MajorCourses { get; set; }
}
If I want to see the Student data I have to create a Students folder and add a new Scaffolding along with a DbContext like your_namespace.Data.StudentContext. I would also have to follow the same steps for the Major entity but am I correct to use another DbContext? For example: your_namespace.Data.MajorContext. This is what I have done but when I try to make migrations and update the database in the your_namespace.Data.MajorContext, I get an error that reads SQLite Error 1: 'table "StudentCourses" already exists'.`
How do I fix this error?
I should also add the Context classes:
This is the StudentContext class:
public class AdvismentContext : DbContext
{
public AdvismentContext(DbContextOptions<AdvismentContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Major> Majors { get; set; }
public DbSet<Advisor> Advisors { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<MajorCourse> MajorCourses { get; set; }
public DbSet <StudentCourses> StudentCourses{ get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.ToTable("Students");
modelBuilder.Entity<Major>()
.ToTable("Majors");
modelBuilder.Entity<Advisor>()
.ToTable("Advisors");
modelBuilder.Entity<Course>()
.ToTable("Courses");
//modelBuilder.Entity<MajorCourse>()
// .ToTable("MajorCourse");
//.HasKey(mc => new { mc.MajorId, mc.CourseId });
}
And this is the MajorContext class:
public class MajorContext : DbContext
{
public MajorContext (DbContextOptions<MajorContext> options)
: base(options)
{
}
public DbSet<Advisment.Models.Major> Major { get; set; } = default!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Major>()
.ToTable("Major");
//modelBuilder.Entity<MajorCourse>() This is commented out because I was getting the same error that the table already exists.
// .ToTable("MajorCourse");
}
}
What am I doing wrong?
I am trying to create a Product table that will have a list of SubstitutionProducts that should reference other Product from the same table.
Model example :
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsMissing { get; set; }
public ICollection<Product> SubstitutionProducts { get; set; }
}
It is better to include the parent ID in your model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsMissing { get; set; }
public int? ParentId { get; set; }
public Product Parent { get; set; }
public ICollection<Product> SubStitutionProducts { get; set; }
}
This is how to configure DB schema via overriding OnModelCreatingmethod of your DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasOne(x => x.Parent)
.WithMany(x => x.SubStitutionProducts)
.HasForeignKey(x => x.ParentId).IsRequired(false);
}
I created some classes and a table in db for those classes with migration. I tested my code by adding some data to the classes,saved by EF and saw the data saved properly in db.
Later when I created a view in db and maped that view in code the problems started. When I tried to query this way:
using (var db = new TestDBContext())
{
var listMyViews = db.vwCustomer.ToList();
}
I was getting an error message like
Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
So EF is telling me to migrate features to create view in db but view exists already. So why do I need to recreate it by migration?
my full code with view mapping and class
public class CustomerBase
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
public class Customer : CustomerBase
{
public virtual List<Addresses> Addresses { get; set; }
}
public class Addresses
{
[Key]
public int AddressID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public bool IsDefault { get; set; }
public virtual List<Contacts> Contacts { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
}
public class Contacts
{
[Key]
public int ContactID { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public bool IsDefault { get; set; }
public int AddressID { get; set; }
public virtual Addresses Customer { get; set; }
}
public class vwCustomer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
}
public class TestDBContext : DbContext
{
public TestDBContext()
: base("name=TestDBContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new vwCustomerConfiguration());
}
public DbSet<Customer> Customer { get; set; }
public DbSet<Addresses> Addresses { get; set; }
public DbSet<Contacts> Contacts { get; set; }
public DbSet<vwCustomer> vwCustomer { get; set; }
}
public class vwCustomerConfiguration : EntityTypeConfiguration<vwCustomer>
{
public vwCustomerConfiguration()
{
this.HasKey(t => t.CustomerID);
this.ToTable("vwCustomer");
}
}
Later I tried to see if any migration is pending issuing Add-Migration "My_vwCustomer". I saw new migration code being added as below. It seems there is no migration pending.
public partial class My_vwCustomer : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.vwCustomers",
c => new
{
CustomerID = c.Int(nullable: false, identity: true),
FirstName = c.String(),
})
.PrimaryKey(t => t.CustomerID);
}
public override void Down()
{
DropTable("dbo.vwCustomers");
}
}
Another way we can do it and it solve my problem. see the code.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<YourDbContext>(null);
base.OnModelCreating(modelBuilder);
}
code taken from here https://stackoverflow.com/a/6143116/6188148
we can follow this approach too.
public partial class AddingvwCustomer : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
i guess this will works too but not tested myself.
we can use the Fluent API to configure it using the Ignore method:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<MyClass>();
}
thanks
Using the Northwind database, I have generated all of the entities from that table using EF6.
Now I would like to create an additional custom entity, which is not an existing table, but it pulls data from three existing tables. The custom entity is "OrderCustomized".
public class OrderCustomized
{
public int OrderID { get; set; }
public DateTime? OrderDate { get; set; }
public string CustomerID { get; set; }
public string CustomerContactName { get; set; }
public string CustomerPhone { get; set; }
public int? EmployeeID { get; set; }
public string EmployeeLastName { get; set; }
public string EmployeeFirstName { get; set; }
}
public partial class NorthwindEntities : DbContext
{
public NorthwindEntities()
: base("name=NorthwindEntities")
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//throw new UnintentionalCodeFirstException();
modelBuilder.Entity<OrderCustomized>()
.Map(m =>
{
m.Properties(o => new { o.OrderID, o.OrderDate, o.CustomerID, o.EmployeeID });
m.ToTable("Order");
})
.Map(m =>
{
m.Properties(c => new { c.CustomerID, c.CustomerContactName, c.CustomerPhone });
m.ToTable("Customer");
})
.Map(m =>
{
m.Properties(e => new { e.EmployeeID, e.EmployeeLastName, e.EmployeeFirstName });
m.ToTable("Employee");
})
;
base.OnModelCreating(modelBuilder);
}
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Contact> Contacts { get; set; }
public virtual DbSet<CustomerDemographic> CustomerDemographics { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Order_Detail> Order_Details { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Region> Regions { get; set; }
public virtual DbSet<Shipper> Shippers { get; set; }
public virtual DbSet<Supplier> Suppliers { get; set; }
public virtual DbSet<Territory> Territories { get; set; }
public virtual DbSet<OrderCustomized> OrdersCustomized { get; set; }
}
I keep getting the error "The entity type OrderCustomized is not part of the model for the current context."
How do I made OrderCustomized part of the model for the current context?
I would really appreciate any sample code, as I cannot find any when searching the internet.
Thanks,
Kellie
Remember to update T4 file (.tt extension). Let me know does it work after that operation.
This is scenario one, which works fine:
public class Domain
{
public int DomainId { get; set; }
[InverseProperty("Domain")]
public virtual ICollection<Person> Persons { get; set; }
[InverseProperty("Domain")]
public virtual ICollection<Group> Groups { get; set; }
}
public class Person
{
public int PersonId { get; set; }
public virtual Domain Domain { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
public class Group
{
public int GroupId { get; set; }
public virtual Domain Domain { get; set; }
public virtual ICollection<Person> Members { get; set; }
}
And this is scenario two, which fails.
public class Domain
{
// Same as scenario 1...
}
public class Person
{
public int PersonId { get; set; }
public int DomainId { get; set; } // <--- new
[ForeignKey("DomainId")] // <--- new
public virtual Domain Domain { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
public class Group
{
public int GroupId { get; set; }
public int DomainId { get; set; } // <--- new
[ForeignKey("DomainId")] // <--- new
public virtual Domain Domain { get; set; }
public virtual ICollection<Person> Members { get; set; }
}
The error message in scenario 2 is the following:
The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.GroupMembers_dbo.Persons_MemberId ]
Both scenarios have this mapping (many to many), inside OnModelCreating's method.
modelBuilder.Entity<Group>()
.HasMany(group => group.Members)
.WithMany(member => member.Groups)
.Map(m =>
{
m.ToTable("GroupMembers");
m.MapLeftKey("GroupId");
m.MapRightKey("MemberId");
});
What am I doing wrong?! What I want to achieve is perfectly reasonable
Either you're using a different version of EF to me (I'm using 5), or you're not including some code which is causing your problem. I created a context containing the code you've provided, and the only error I got was an error about multiple cascade paths (Domains being deleted via Persons or via Groups). I removed Cascade delete on one of the relationships, and it works fine.
Here's my entire context class which works with no errors:
public class TestContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Domain> Domains { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Group>()
.HasMany(group => group.Members)
.WithMany(member => member.Groups)
.Map(m =>
{
m.ToTable("GroupMembers");
m.MapLeftKey("GroupId");
m.MapRightKey("MemberId");
});
modelBuilder.Entity<Domain>()
.HasMany(d => d.Groups).WithRequired(g => g.Domain)
.WillCascadeOnDelete(false);
}
}