I've created a Model using Model first and Entity Data Model of Entity Framework. Well, when I want to create the controller (right click on controllers folder Add->controller->WebApi 2 Controller with actions using EF) then I become after defining the inputfields an error message:
there was an error getting the type "WebApi.Models.QR_Name". Try rebuilding the project. The same error getting by the other Model class. How can I solve this??
EDIT:
I have two classes:
//Group
namespace WebApi.Models
{
public partial class QR_Group
{
public QR_Group()
{
this.QR_Name = new HashSet<QR_Name>();
}
public int Id { get; set; }
public string name { get; set; }
public string code { get; set; }
public virtual ICollection<QR_Name> QR_Name { get; set; }
}
}
//Name
namespace WebApi.Models
{
public partial class QR_Name
{
public int Id { get; set; }
public string firstname { get; set; }
public double maxAge { get; set; }
public int QR_GroupId { get; set; }
public virtual QR_Group QR_Group { get; set; }
}
}
Additionally is here the Context class:
namespace WebApi.Models
{
public partial class WebApiContext : DbContext
{
public WebApiContext()
: base("name=WebApiContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<QR_Group> QR_Groups { get; set; }
public virtual DbSet<QR_Name> QR_Names { get; set; }
}
}
Before creating your controller, press Ctrl+Shift+B to build your solution or go to 'Build->Build Solution' and then try creating your controller.
Related
I have a class User in a class library as follows:
Superclass called Base:
namespace Shared_class_library.Models;
public abstract class Base
{
public Base()
{
LastUpdated = DateTime.Now;
CreatedDate = CreatedDate ?? LastUpdated;
}
public Guid Id { get; set; }
public DateTime? LastUpdated { get; set; }
public DateTime? CreatedDate { get; set; }
}
Then a class called User which inherits from Base:
namespace Shared_class_library.Models;
public class User : Base
{
private User()
{
}
public User(string name, string email)
{
Name = name;
}
public string Name { get; set; }
public List<DietaryRequirement>? DietaryRequirements { get; set; }
public Squad? Squad { get; set; }
public List<ReferenceChampion>? ChampionRoles { get; set; }
}
I have a class called ReferenceChampions:
namespace Shared_class_library.Models
{
public class ReferenceChampion : Base
{
public ReferenceChampion()
{
}
public ReferenceChampion(string name)
{
Name = name;
}
public string Name { get; set; }
public List<User> users { get; set; }
}
}
I want there to be a many to many relationship between User and Champion, i.e a user can be many different type of champion, and each type of champion can have many different users.
I define this in my DbContext like this:
modelBuilder.Entity<User>()
.HasMany(u => u.ChampionRoles)
.WithMany(u => u.users);
The problem I am having is when I run the migration command
dotnet ef migrations add firstmigration
and then
dotnet ef database update
I get this error:
Cannot find the object "DietaryRequirements" because it does not exist or you do not have permissions.
The class in question DietaryRequirements is:
namespace Shared_class_library.Models;
public class DietaryRequirement : Base
{
private DietaryRequirement()
{
}
public DietaryRequirement(string requirement)
{
Requirement = requirement;
}
public string Requirement { get; set; }
}
I'm not sure why this error is coming up as there isn't any relationship here between ReferenceChampion and DietaryRequirement.
Any ideas why?
I'm using Entity Framework to generate my database model for my application, and now I'm trying to submit some data into my database but I'm getting the following error:
The entity type SEC_USER is not part of the model for the current context.
I have read a lot that sometimes this error is shown because the connection string is wrong, but I know that my connection string is fine because the first view that is shown in my application brings a lot of data from the database and it works perfectly.
The code that is throwing me the error is the following, specifically the line db.SEC_USER.Add(user);:
public ActionResult LogIn(Login log)
{
bool enter = _2Secure.Common.Data.Access.ldap.SignIn(log.Username, log.Password);
ViewBag.entra = enter;
if (enter)
{
SEC_USER user = new SEC_USER();
user.name = "Yesid Bejarano";
user.isAdmin = 1;
db.SEC_USER.Add(user);
db.SaveChanges();
return View("~/Views/Home/_GridViewPartial.cshtml");
}
else
return View("~/Views/Login/LogIn.cshtml");
}
And my DbContext have this structure:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class VASARODEVEnti : DbContext
{
public VASARODEVEnti()
: base("name=VASARODEVEnti")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SEC_USER>().ToTable("SEC_USER");
}
public virtual DbSet<SEC_ACTION> SEC_ACTION { get; set; }
public virtual DbSet<SEC_ROL> SEC_ROL { get; set; }
public virtual DbSet<SEC_USER> SEC_USER { get; set; }
public virtual DbSet<VAS_CATEGORY> VAS_CATEGORY { get; set; }
public virtual DbSet<VAS_PROYECT> VAS_PROYECT { get; set; }
public virtual DbSet<VAS_PROYECT_VULNERABILITY> VAS_PROYECT_VULNERABILITY { get; set; }
public virtual DbSet<VAS_PROYECT_VULNERABILITY_IP> VAS_PROYECT_VULNERABILITY_IP { get; set; }
public virtual DbSet<VAS_PROYECT_VULNERABILITY_IP_PORT> VAS_PROYECT_VULNERABILITY_IP_PORT { get; set; }
public virtual DbSet<VAS_PROYECT_VULNERABILITY_IP_URLS> VAS_PROYECT_VULNERABILITY_IP_URLS { get; set; }
public virtual DbSet<VAS_VULNERABILITY> VAS_VULNERABILITY { get; set; }
public virtual DbSet<VAS_VULNERABILITY_ENG> VAS_VULNERABILITY_ENG { get; set; }
public virtual DbSet<VAS_VULNERABILITY_PARENT> VAS_VULNERABILITY_PARENT { get; set; }
public virtual DbSet<VAS_VULNERABILITY_VULNERABILITY_PARENT> VAS_VULNERABILITY_VULNERABILITY_PARENT { get; set; }
}
I just don't know what else can be causing the error.
SOLVED:
The columns of the table in the database and the entity were different. I Re-Create the model with the right columns and now is working.
Whenever I attempt to create a new scaffolded item for my project, I receive an error stating
There was an error creating the DbContect instance to get the model
No Parameterless constructor defined for this object.
I have attempted to rebuild my project, and I have had success generating scaffolds prior, however for some reason when doing so now I haven't had any success. No alterations have been done to my context class since I last added a new scaffolded item.
I have attempted to create an empty constructor in my model however it still tosses the same error. Below you will find my code for my model I'm trying to scaffold, and my context.
namespace HRIAT.Models
{
public class Credential
{
public Credential() { }
public int ID { get; set; }
public string credentialName { get; set; }
public FundingSource credentialFundingSource { get; set; }
public string credentialType { get; set; }
public float credentialCost { get; set; }
public ICollection<Employee> ec { get; set; }
}
}
Context class:
namespace HRIAT.Models
{
public class HRIATContext : DbContext
{
public HRIATContext(DbContextOptions<HRIATContext> options)
: base(options)
{
}
public DbSet<FundingSource> FundingSource { get; set; }
public DbSet<Credential> Credential { get; set; }
public DbSet<Employee> Employee { get; set; }
public DbSet<EmployeesCredentials> EmployeeCredential { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeesCredentials>().HasKey(c => new { c.CredentialID, c.employeeNum });
}
public DbSet<Position> Positions { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Location> Locations { get; set; }
}
}
This error appears if the entities of your project do not contains an empty constructor. EntityFramework needs the empty constructor to instantiate the entity and maps properties of schema.
If you needs protect the entity classes, use the protected constructor, example:
public class Person
{
public string Name { get; private set; }
public Person(string name)
{
Name = name;
}
protected Person() { }
}
This is the content of my DivorceCases.cs file inside Models:
public class DivorceCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class DivorceCasesContext : DbContext
{
public DivorceCasesContext() : base("mssqlDB") { }
public DbSet<DivorceCases> DivorceCase { get; set; }
}
This is the content of my CorporationCases.cs file inside Models:
public class CorporationCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class CorporationCasesContext : DbContext
{
public CorporationCasesContext() : base("mssqlDB") { }
public DbSet<CorporationCases> CorporationCase { get; set; }
}
Now my question is:
I am using code-first approach to let EF6 create table for me automatically.
when I try to create and use any instance of table and EF6 creates it as well for DivorceCases Model and Context pair. But after the DivorceCases table has been created, I try to create and use CorporationCases Model instance then EF6 fails to automatically create the table for me because
"Transaction" Table has already been created by DivorceCases context
So how do I solve this issue?
You don't need to create 2 dbContext here.just use one as shown below.
Important Note : You're not following basic naming conventions.I highly recommend to use those.
one place is here : public DbSet<DivorceCases> DivorceCase { get; set; }
need to be corrected as public DbSet<DivorceCase> DivorceCases { get; set; }
Using One DbContext :
public class DivorceCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class CorporationCases
{
public string case_id { get; set; }
public virtual Transactions t { get; set; }
}
public class YourCasesContext : DbContext
{
public YourCasesContext () : base("mssqlDB") { }
public DbSet<DivorceCases> DivorceCase { get; set; }
public DbSet<CorporationCases> CorporationCase { get; set; }
}
When I create the Scaffold for Controller and add the Model class then I am getting these error "Multiple object sets per type are not supported".
I have three Model class :
1.Department.CS
2.Designation.cs
3.CompanyDBContext.cs
Database : I have two table in database, 1. Department(deptID,deptName,Description) 2. Designation(desgtID,desgName,description)
Objective :- I want to create one view page for these scenario. Like this
Insert Name of Form (TextBox) + Department Name (Dropdown list box) + Designation Name (Dropdown list box)
1.Department.CS
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
}
2.Designation.cs
namespace mvcAppraisalSystem.Models
{
public class Designation
{
[Key]
public int desgID { get; set; }
public string desgName { get; set; }
public string description { get; set; }
}
}
3.CompanyDBContext.cs
namespace mvcAppraisalSystem.Models
{
public class CompanyDBContext : DbContext
{
public CompanyDBContext() : base("CompanyDBContext")
{
}
public DbSet<CompanyDBContext> Departments { get; set; }
public DbSet<CompanyDBContext> Designations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
You're creating your sets to be DbSet<CompanyDBContext>. What you want is DbSet<Department> and DbSet<Designation>.
public DbSet<Department> Departments { get; set; }
public DbSet<Designation> Designations { get; set; }
This seems pretty clearly to be a typo, but the reason you're getting the error is because the runtime wouldn't know how to populate multiple object sets on the same context that have the same element type. This would be more like saying:
public DbSet<Department> SomeDepartments { get; set; }
public DbSet<Department> OtherDepartments { get; set; }
Since (presumably) you would expect something to define what would be in SomeDepartments and there would be something to define what's in OtherDepartments and the runtime doesn't know this (and there's no way to express it), that's why you're getting the error.