Here i'm using EntityFramework for inserting data into two different tables. Please suggest me how can i do
ClassFile
namespace ImageWithallCtrls.Models
{
public partial class TwoTabes
{
public partial class Employee
{
public int id { get; set; }
public string Name { get; set; }
public string EMail { get; set; }
public string Password { get; set; }
public virtual ICollection<Department> Department { get; set; }
}
public class Department
{
public int DID { get; set; }
public string D_Name { get; set; }
public string Employee_Name { get; set; }
public Nullable<decimal> Salary { get; set; }
}
}
}
HomeController.cs
public ActionResult InserTwise(TwoTabes model )
{
Employee emp = db.Employees.Create();
emp.Name = model.Employee.Name;
}
Here i'm Getting Error "model.Employee.Name"
Related
I have these tables: Sales, product, stock and salesProduct. I'm using a database first approach, they were created using EF scaffold. Sales table looks like this:
public partial class Sales
{
public Sales()
{
SalesProduct = new HashSet<SalesProduct>();
}
public int Id { get; set; }
public DateTime? CreationDate { get; set; }
public bool IndActive { get; set; }
public virtual ICollection<SalesProduct> SalesProduct { get; set; }
}
}
I need the post method on sales creation to create a SalesProduct object and add it to the database. I also need it to update the stock of that specific product.
Here are the other tables:
public partial class SalesProduct
{
public int Id { get; set; }
public int SaleId { get; set; }
public int ProductId { get; set; }
public decimal? Value { get; set; }
public bool IndActive { get; set; }
public virtual Produto IdProductNavigation { get; set; } = null!;
public virtual Vendum IdSalesNavigation { get; set; } = null!;
}
}
public partial class Stock
{
public int Id { get; set; }
public int IdProduct { get; set; }
public int? Quantitu { get; set; }
public bool IndActive { get; set; }
public virtual Product IdProductNavigation { get; set; } = null!;
}
}
How can i do that? I'm confused on how to deal with entity relationships in http methods!
What could be wrong here? I have seen others include multilevel class to Lambda expression using Select, but I'm getting this error. Can anyone help please?
public partial class Employee
{
public Employee()
{
EmployeeRole = new HashSet<EmployeeRole>();
}
public int Id { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public virtual ICollection<EmployeeRole> EmployeeRole { get; set; }
}
public partial class EmployeeRole
{
public int EmployeeId { get; set; }
public int RoleId { get; set; }
public virtual Employee Employee { get; set; }
public virtual Role Role { get; set; }
}
public partial class Role
{
public Role()
{
EmployeeRole = new HashSet<EmployeeRole>();
}
public int Id { get; set; }
public string Role1 { get; set; }
public virtual ICollection<EmployeeRole> EmployeeRole { get; set; }
}
public async Task<IActionResult> Index()
{
return View(await _context.Employee.Include(a=>a.EmployeeRole.Select(r=>r.Role)).ToListAsync());
}
I have been reading about Entity Framework during the pass few weeks. I came across TPT subject. I am a little bit confused and have difficulty to differentiate between TPT and Navigation. When should we choose one over another. Please take a look at the code below.
A. Navication
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public List<ContractEmployee> ContractEmployees { get; set; }
public List<PermanentEmployee> PermanentEmployees { get; set; }
}
public class ContractEmployee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
public Employee Employee { get; set; }
}
public class PermanentEmployee
{
public int AnnualSalary { get; set; }
public Employee Employee { get; set; }
}
B. TPT
[Table("Employee")]
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
}
[Table("ContractEmployee")]
public class ContractEmployee : Employee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
}
[Table("ContractEmployee")]
public class PermanentEmployee : Employee
{
public int AnnualSalary { get; set; }
}
I am using a objectDataSource to populate a gridview. I have 2 simple classes :
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public Department Department { get; set; }
}
and
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public List<Employee> Employees { get; set; }
}
I also have
public class EmployeeDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
}
now in my EmployeeRepository class i have
public List<Department> GetDepartments()
{
EmployeeDBContext employeeDBContext = new EmployeeDBContext();
return employeeDBContext.Departments.Include("Employees").ToList();
}
Even though I have added .Include("Employees"), the employees are missing in gridview. what am I doing wrong here?
First of all, you will need to have a foreign key (DepartmentId) inside Employee class. I do not know how the video got away with that.
public class Employee
{
public int Id { get; set; }
public int DepartmentId { get; set; } <=====
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public virtual Department Department { get; set; }
^^^^^^^
}
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
^^^^^^^^^^^^^^^^^^
}
public partial class EmployeeDBContext : DbContext
{
public virtual DbSet<Employee> Employee { get; set; }
public virtual DbSet<Department> Department { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Optional, but good practice to have the mapping here.
modelBuilder.Entity<Department>()
.HasMany(e => e.Employee)
.WithRequired(e => e.Department)
.HasForeignKey(e => e.DepartmentId);
}
}
- OR -
Add DepartmentId property and [ForeignKey] data annotation to Department.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; } <===
// Optional, but good practice to have this data annotation attribute.
[ForeignKey("DepartmentId")] <===
public Department Department { get; set; }
}
FYI: You want to use virtual, in case someone wants to use lazy loading in the future.
have you tried something like this
return employeeDBContext.Departments.Include(x =>x.Employees ).ToList(); ?
I have an ASP.NET MVC project supporting multiple languages.
the tables (User,Gender,GenderTranslate) had nested inheritance
User inherit from Gender and Gender inherit from GenderTranslate
I have these tables
User table :
public partial class User
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable<int> GenderID { get; set; }
public virtual Gender Gender { get; set; }
}
and Gender table:
public partial class Gender
{
public Gender()
{
this.GenderTranslates = new HashSet<GenderTranslate>();
this.Users = new HashSet<User>();
}
public int Id { get; set; }
public string Icon { get; set; }
public string Value { get; set; }
public virtual ICollection<GenderTranslate> GenderTranslates { get; set; }
public virtual ICollection<User> Users { get; set; }
}
and Language table:
public partial class Lang
{
public Lang()
{
this.GenderTranslates = new HashSet<GenderTranslate>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GenderTranslate> GenderTranslates { get; set; }
}
and GenderTranslate table :
public partial class GenderTranslate
{
public int LangID { get; set; }
public int GenderID { get; set; }
public string Desc { get; set; }
public virtual Gender Gender { get; set; }
public virtual Lang Lang { get; set; }
}
When I get users I need to get GenderTranslate.Desc, not Gender.Value.
How can I do that using Entity Framework? Please help me