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());
}
Related
How can I save data to junction table from controller?
public ActionResult Create(KreatorZamowienNabywca model)
{
db.KreatoryZamowien.Add(model.KreatorZamowien);
db.Nabywcy.Add(model.Nabywca);
db.SaveChanges();
}
My database schema:
KreatorZamowien(Id, NumerZamowienia) -------------------------------|
|
NabywcaKreatorzamowien (KreatorZamowien_Id, Nabywca_Nabywca_Id)------|
|
Nabywca(Nabywca_Id, Nazwa) ------------------------------------------|
KreatorZamowien.cs
public class KreatorZamowien
{
public int Id { get; set; }
public int NumerZamowienia { get; set; }
public virtual ICollection<Nabywca> Nabywcy { get; set; }
}
Nabywca.cs
public class Nabywca
{
public int NabywcaId { get; set; }
public string Nazwa { get; set; }
public virtual ICollection<KreatorZamowien> KreatoryZamowien { get; set; }
}
Model containing 2 models to display them in View:
KreatoryZamowienNabywca.cs
public class KreatorZamowienNabywca
{
public KreatorZamowien KreatorZamowien { get; set; }
public Nabywca Nabywca { get; set; }
}
Just cross reference both objects.
public class KreatorZamowien
{
public int Id { get; set; }
public int NumerZamowienia { get; set; }
public virtual ICollection<Nabywca> Nabywcy { get; set; } = new Hashset<Nabywca>();
}
public class Nabywca
{
public int NabywcaId { get; set; }
public string Nazwa { get; set; }
public virtual ICollection<KreatorZamowien> KreatoryZamowien { get; set; } = new Hashset<KreatorZamowien>();
}
public ActionResult Create(KreatorZamowienNabywca model)
{
model.KreatorZamowien.Nabywcy.Add(model.Nabywca);
model.Nabywca.KreatoryZamowien.Add(model.KreatorZamowien);
db.KreatoryZamowien.Add(model.KreatorZamowien);
db.Nabywcy.Add(model.Nabywca);
db.SaveChanges();
}
I have tow classes ( RolesDTO and Roles) in RolesDTO i have List<int> idFonction and in Roles i have List<functions> functions :
Class RolesDTO
public class RolesDTO
{
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<int> IdFunctions { get; set; }
}
Class Role
public partial class Roles
{
public Roles()
{
this.Functions = new HashSet<Functions>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<Functions> Functions { get; set; }
}
How can i mapping List<int> idFunctions to List<funtions> functions with automapper ?
class function
public partial class Functions
{
public int Id { get; set; }
public string Description { get; set; }
public bool Actif { get; set; }
}
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"
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
I have few Domain Models - Address, Customer, Employee, StoreLocation. Address has many to one relationship with Customerand Employee and one to one relationship with StoreLocation.
public class Address
{
public int Id;
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Address> Addresses { get; set; }
}
public class StoreLocation
{
public int Id { get; set; }
public string ShortCode { get; set; }
public string Description { get; set; }
public Address Address { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dob { get; set; }
public IList<Address> Addresses { get; set; }
}
How to Map this relationship?. I am using ASP.NET MVC 3.0 and Entity Framework 4.1.
If you are using code-first (I think you want this, else, you have to edit your Q), the first way is the way explained below:
Entities:
public class Address {
[Key]
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public virtual Customer Customer { get; set; }
public virtual StoreLocation StoreLocation { get; set; }
public virtual Employee Employee { get; set; }
public int? CustomerId { get; set; }
public int? EmployeeId { get; set; }
}
public class Customer {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class StoreLocation {
[Key]
public int Id { get; set; }
public string ShortCode { get; set; }
public string Description { get; set; }
public virtual Address Address { get; set; }
}
public class Employee {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dob { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
DbContext inherited class:
public class ManyOneToManyContext : DbContext {
static ManyOneToManyContext() {
Database.SetInitializer<ManyOneToManyContext>(new ManyOneToManyInitializer());
}
public DbSet<Address> Addresses { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<StoreLocation> StoreLocations { get; set; }
public DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Entity<Customer>().HasMany(c => c.Addresses).WithOptional(a => a.Customer).HasForeignKey(a => a.CustomerId);
modelBuilder.Entity<StoreLocation>().HasRequired(s => s.Address).WithOptional(a => a.StoreLocation).Map(t => t.MapKey("AddressId"));
modelBuilder.Entity<Employee>().HasMany(e => e.Addresses).WithOptional(a => a.Employee).HasForeignKey(e => e.EmployeeId);
}
}
Context Initializer:
public class ManyOneToManyInitializer : DropCreateDatabaseAlways<ManyOneToManyContext> {
protected override void Seed(ManyOneToManyContext context) {
}
}
That will create the db-schema below:
Let me know if you have any questions or need clarifications on any part.