How do I fix this SQLite error: SQLite Error 1: 'table "StudentCourses" already exists' - entity-framework

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?

Related

How to configure one-to-one relations to the same table in Entity Framework Core

I have a model as follows:
public class Category : BaseEntity
{
public string Name { get; set; }
public virtual Category Parent { get; set; }
public string Description { get; set; }
}
Parent property is related to the same table. How can i configure it?
I'm imagining something like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasOne(a => a.Parent)
.OnDelete(false);
}
I'm newbie with Entity framework core, please help me !!!
Resoled by:
public class Category : BaseEntity
{
public string Name { get; set; }
public int? ParentId{ get; set; } /*added*/
[ForeignKey("ParentId")] /*added*/
public virtual Category Parent { get; set; }
public string Description { get; set; }
}

EF.core One to many referencing the same table

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);
}
}

Can we mix new table and existing view in EF code first

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

EF6: How to make Custom Entity from Multiple Tables

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.

EF 4.1 code first still looking for old table that no longer exists

I am using Entity Framework 4.1 code first.
I had a table in the database with the name MaritalStatus. I deleted it and created a new table in its place called MaritalStatuses. Whenever I try to get all the records from the table I get an error:
Invalid object name 'dbo.MaritalStatus'.
The query that it is trying to execute is:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[MaritalStatus] AS [Extent1]}
Why would it still be looking for table MaritalStatus that I deleted? Can it be possible that it is cached somewhere? I dropped the whole database and recreated it via scripts. Still no luck. Maybe it has an issue when it comes to the "es" part of the name?
Context class:
public class HefContext : DbContext
{
public DbSet<Bank> Banks { get; set; }
public DbSet<AccountType> AccountTypes { get; set; }
public DbSet<MaritalStatus> MaritalStatuses { get; set; }
}
View model (with partial properties):
public class EditGrantApplicationViewModel
{
public int Id { get; set; }
public string EmployeeNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int MaritalStatusId { get; set; }
public IEnumerable<MaritalStatus> MaritalStatuses { get; set; }
public int BankId { get; set; }
public IEnumerable<Bank> Banks { get; set; }
public int AccountTypeId { get; set; }
public IEnumerable<AccountType> AccountTypes { get; set; }
}
Dropdown list in view for MaritalStatuses:
<td><label>Marital Status:</label> <span class="red">**</span></td>
<td>
#Html.DropDownListFor(x => x.MaritalStatusId, new SelectList(Model.MaritalStatuses, "Id", "Name", Model.MaritalStatusId), "-- Select --")
#Html.ValidationMessageFor(x => x.MaritalStatusId)
</td>
Controller:
public ActionResult Create()
{
EditGrantApplicationViewModel viewModel = new EditGrantApplicationViewModel
{
MaritalStatuses = maritalStatusService.GetAll(),
Banks = bankService.GetAll(),
AccountTypes = accountTypeService.GetAll()
};
return View(viewModel);
}
Service:
public IEnumerable<MaritalStatus> GetAll()
{
return maritalStatusRepository.GetAll();
}
Repository:
HefContext db = new HefContext();
public IEnumerable<MaritalStatus> GetAll()
{
return db.MaritalStatuses;
}
Model class:
public class MaritalStatus
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
You may have renamed the table in your database, but your Model class is still tied to the old name. You need to map the entity to the desired table name in the OnModelCreating method of your DbContext object
public class HefContext : DbContext
{
public DbSet<Bank> Banks { get; set; }
public DbSet<AccountType> AccountTypes { get; set; }
public DbSet<MaritalStatus> MaritalStatuses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MaritalStatus>().ToTable("MaritalStatuses");
}
}
Update:
Apparently EF code first has some issues with the pluralization of some table names, such as Status. Out of curiosity I tested a few others and found several which also had this same issue.
I guess this is wrong:
public DbSet<MaritalStatus> MaritalStatuses { get; set; }
Should be:
public DbSet<MaritalStatuses> MaritalStatuses { get; set; }