Join two tables in EntityFramework - mysql

I have two tables:
Companies
Contacts with a one-to-many relations (many contacts in one Company)
Contacts table EDITED:
public class Contact
{
public Guid Id { get; set; }
public DateTime dateCreated { get; set; }
public DateTime updated { get; set; }
public Boolean hidden { get; set; }
//Personal Data
public string title { get; set; }
public string jobTitle { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string department { get; set; }
public string email { get; set; }
public string logoUrl { get; set; }
public string personalRemarks { get; set; }
//Telephone list
public ICollection<Phone> Phones { get; set; }
//Addresses
public ICollection<Address> Addresses { get; set; }
//Bank Data
public ICollection<Bankdata> Bankdatas { get; set; }
//Tags
public ICollection<Tag> Tags { get; set; }
}
}
Companies table EDITED:
public class Organization
{
public Guid Id { get; set; }
public DateTime dateCreated { get; set; }
public DateTime dateUpdated { get; set; }
public Boolean hidden { get; set; }
//Company Data
public string organizationName { get; set; }
public string taxId { get; set; }
public string trades { get; set; }
public string organizationType { get; set; }
public string actionRadius { get; set; }
public string organizationRemarks { get; set; }
public string web { get; set; }
//Contacts
public ICollection<Contact> Contacts { get; set; }
//Tags
public ICollection<Tag> Tags { get; set; }
}
}
I have a method im my repository to select all the contacts in one company
public Organization GetOrganizationById(Guid Id)
{
return _context.Organizations
.Include(c => c.Contacts)
.Where(c => c.Id == Id)
.FirstOrDefault();
}
But since all I have is the company id I need to make a join between the two tables in order to get the name. Something like:
SELECT contacts,*, Organization.name
FROM contacts
INNER JOIN Organization ON Organization.id = Contacts.organization_id
WHERE Organization.id = id;
I have tried the following without success:
public Organization GetOrganizationById(Guid Id)
{
return _context.Organizations
.Include(o => o.organizationName)
.Include(c => c.Contacts)
.Where(c => c.Id == Id)
.FirstOrDefault();
}
Any help will be welcome

The right approach is to create a navigation property between the Organization class and the Contact class. The following code will let you understand the steps you need to do:
public class Contact
{
...
// Foreign key for Organization
public Guid OrganizationId { get; set; }
// Related Organization entity
[ForeignKey("OrganizationId ")]
public Organization Standard { get; set; }
}
public class Organization
{
...
// List of related Contacts
public virtual ICollection<Contact> Contacts { get; set; }
}
After creating a migration with this code you will implement your method as follows:
public Organization GetOrganizationById(Guid Id)
{
return _context.Organizations
.Include(c => c.Contacts)
.Where(c => c.Id == Id)
.FirstOrDefault();
}

Related

Mapping table is empty

Below are two tables that has a many to many relation and and also another table that has a relation with the two first. A mapping table is created by Visual Studio with the name OrderEmployee, but when I run the application and enter some information to the Timeunit create form, the mapping table OrderEmployee is empty!
Should there not be some IDs added to that table since it has a relation with the Timeunit table or how is this mapping table thing working, when will there be data added to the mapping table? Or is something wrong with my Entity Classes?
public class Order
{
public int ID { get; set; }
public string Name { get; set; }
public int? ManufacturerID { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int EmployeeNumber { get; set; }
public virtual ICollection<Timeunit> Timeunits { get; set; }
public virtual ICollection<Order> Order { get; set; }
}
public class Timeunit
{
public int ID { get; set; }
public int Week { get; set; }
public int HoursPerWeek { get; set; }
public int EmployeeID { get; set; }
public int? OrderID { get; set; }
public virtual Employee Employee { get; set; }
public virtual Order Order { get; set; }
}
EDIT:
Create Method for Timeunit:
public ActionResult Create([Bind(Include = "ID,Week,HoursPerWeek,EmployeeID,OrderID")] Timeunit timeunit)
{
if (ModelState.IsValid)
{
db.Timeunits.Add(timeunit);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProjectID = new SelectList(db.Orders, "ID", "Name", timeunit.OrderID);
ViewBag.ResourceID = new SelectList(db.Employees, "ID", "Name", timeunit.EmployeeID);
return View(timeunit);
}

Fluent API not creating Identity Specification (Is Identity) in SQL Server

I have an MVC4 application that is using code first. I have created 3 models that I wish to use to keep track of my clients Associates each with a designation of AssociateType (Distributors or Retailers) each Associate also has a deignated Region (North Florida or South Florida.
I started off by creating the following models and running update-database to create the tables in the database.
Associate.cs
namespace XXX.Models
{
public class Associate
{
public int AssociateID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AssociateName { get; set; }
public int AddressNumber { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AddressStreet { get; set; }
[StringLength(20), Column(TypeName = "varchar")]
public string AddressCity { get; set; }
[StringLength(2), Column(TypeName = "varchar")]
public string State { get; set; }
[StringLength(10), Column(TypeName = "varchar")]
public string Zipcode { get; set; }
[StringLength(16), Column(TypeName = "varchar")]
public string MainPhoneNumber { get; set; }
[StringLength(60), Column(TypeName = "varchar")]
public string AssociateEmail { get; set; }
[StringLength(80), Column(TypeName = "varchar")]
public string AssociateWebsite { get; set; }
//See Corresponding Navigation Properties
[Display(Name = "Region")]
public int RegionID { get; set; }
[Display(Name = "AssociateType")]
public int AssociateTypeID { get; set; }
[StringLength(35), Column(TypeName = "varchar")]
public string ContactFirstName { get; set; }
[StringLength(35), Column(TypeName = "varchar")]
public string ContactLastName { get; set; }
[StringLength(16), Column(TypeName = "varchar")]
public string ContactPhoneNumber { get; set; }
[StringLength(60), Column(TypeName = "varchar")]
public string ContactEmail { get; set; }
public virtual Region Region { get; set; }
public virtual AssociateType AssociateType { get; set; }
}
AssociateType.cs
namespace XXX.Models
{
public class AssociateType
{
[ForeignKey("Associate")]
public int AssociateTypeID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AssociateTypeName { get; set; }
public virtual Associate Associate { get; set; }
}
}
'Region.cs'
namespace XXX.Models
{
public class Region
{
public int RegionID { get; set; }
[StringLength(20), Column(TypeName = "varchar")]
public string RegionName { get; set; }
[Column(TypeName = "varchar(Max)")]
public string RegionDescription { get; set; }
public virtual Associate Associate { get; set; }
}
}
DBContext
namespace XXX.Models
{
public class XXXDb : DbContext
{
public XXXDb(): base("name=DefaultConnection")
{
}
public DbSet<Associate> Associates { get; set; }
public DbSet<AssociateType> AssociateType { get; set; }
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Associate>().HasKey(a => a.AssociateID);
modelBuilder.Entity<Associate>().Property(a => a.AssociateID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Associate>().HasRequired(at => at.AssociateType)
.WithRequiredDependent();
modelBuilder.Entity<Associate>().HasRequired(r => r.Region)
.WithRequiredDependent();
modelBuilder.Entity<AssociateType>().HasKey(at => at.AssociateTypeID);
modelBuilder.Entity<AssociateType>().Property(at => at.AssociateTypeID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Region>().HasKey(r => r.RegionID);
modelBuilder.Entity<Region>().Property(r => r.RegionID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
}
My Tables are not being created with the IDENTITY SPECIFICATION (Is Identity) being set to Yes..... WHY IS THIS?? Because of this I cannot add any data to the tables or I get an error:
Cannot insert the value NULL into column 'RegionID', table 'XXXDb.dbo.Regions'; column does not allow nulls. INSERT FAILS.
My goal is to populate the Region & AssociateType tables with just a few rows of items.
Regions: (North Florida & South Florida)
AssociateTypes (Distributors & Retailers)
This way when I add an Associate during a CRUD operation I would have two drop downs that have the options (Distributors & Retailers) for AssociateType and (N or S Florida) for that associates Region.
Any help would be very much appreciated. I'm really getting frustrated with MVC. I have made it pretty far, but starting to get discouraged.
I did some test and here is the solution that work on my machine I just kept the navigation properties of your obejcts.
public class Associate
{
public int AssociateID { get; set; }
public int RegionID { get; set; }
public virtual Region Region { get; set; }
public int AssociateTypeID { get; set; }
public virtual AssociateType AssociateType { get; set; }
}
public class Region
{
public int RegionID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string IngredientNameEn { get; set; }
[Column(TypeName = "varchar(Max)")]
public string IngredientNameEs { get; set; }
public virtual List<Associate> Associates { get; set; }
}
public class AssociateType
{
public int AssociateTypeID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AssociateTypeName { get; set; }
public virtual List<Associate> Associates { get; set; }
}
Then in the OnModelCreating you have to add the following two commands and this should generate the database that you want
modelBuilder.Entity<Region>().HasMany(a => a.Associates)
.WithRequired(r => r.Region).HasForeignKey(r => r.RegionID);
modelBuilder.Entity<AssociateType>().HasMany(a => a.Associates)
.WithRequired(r => r.AssociateType).HasForeignKey(r => r.AssociateTypeID);
and in the class constructor you may add this code
public XXXDb(): base("name=DefaultConnection")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<XXXDb>());
Database.Initialize(force: true);
}

Code first generating strange column

I have an MVC 4 application that is using code first to generate tables and columns in my SQL Server DB. I am trying to figure out how I ended up with an additional TABLE that was not intended. I have looked through some questions but not found the exact same problem I am having. I will try to explain this simply.
I have added a model called Associate which keeps track of associates that my client does business with. Each Associate needs a foriegn key of AssociateTypedID and RegionID.
namespace XXX.Models
{
public class Associate
{
public int AssociateId { get; set; }
public string AssociateName { get; set; }
public int AddressNumber { get; set; }
public string AddressStreet { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public string MainPhoneNumber { get; set; }
public string AssociateEmail { get; set; }
public string AssociateWebsite { get; set; }
public string ContactFirstName { get; set; }
public string ContactLastName { get; set; }
public string ContactPhoneNumber { get; set; }
public string ContactEmail { get; set; }
public int RegionId { get; set; }
public int AssociateTypeId { get; set; }
public virtual ICollection<AssociateType> AssociateTypes { get; set; }
public virtual ICollection<Region> Regions { get; set; }
}
}
AND
namespace XXX.Models
{
public class AssociateType
{
public int AssociateTypeId { get; set; }
public string AssociateTypeName { get; set; }
public virtual ICollection<Associate> Associates { get; set; }
}
}
AND
namespace XXX.Models
{
public class Region
{
public int RegionId { get; set; }
public int RegionName { get; set; }
public int RegionDescription { get; set; }
public virtual ICollection<Associate> Associates { get; set; }
}
}
AND
namespace XXX.Models
{
public class XXXDb : DbContext
{
public XXXDb(): base("name=DefaultConnection")
{
}
public DbSet<Associate> Associates { get; set; }
public DbSet<AssociateType> AssociateTypes { get; set; }
public DbSet<Region> Regions { get; set; }
}
}
So I have updated my code above and I'm getting very close to where I need to be in my database. I have the following tables generated.
Associates, AssociateTypes & Regions (each of them have the columns I would expect)
BUT I now have a new table called RegionAssociates which has the following columns:
Region_RegionId (int) & Associate_AssociateId (int)
This table was not expected or needed in my schema.
Your classes doesn't match your description of the model. You are saying
Each Associate can have a designation of AssociateType
I suppose that the same AssociateType can be assigned to more Associates, so there should be 1:N relationship between AssociateType and Associate.
But the Associate class defines the relationship the other way around - by convention public virtual ICollection<AssociateType> AssociateType { get; set; } creates 1:N relationship between Associate and AssociateType.
the correct definition of your classes would be
public class Associate
{
public int AssociateId { get; set; }
public string AssociateName { get; set; }
public int AddressNumber { get; set; }
public string AddressStreet { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public string MainPhoneNumber { get; set; }
public string AssociateEmail { get; set; }
public string AssociateWebsite { get; set; }
public int RegionId { get; set; }
public int AssociateTypeId { get; set; }
public virtual AssociateType AssociateType { get; set; }
public string ContactFirstName { get; set; }
public string ContactLastName { get; set; }
public string ContactPhoneNumber { get; set; }
public string ContactEmail { get; set; }
public virtual ICollection<Region> Regions { get; set; }
}
public class AssociateType
{
public int AssociateTypeId { get; set; }
public string AssociateTypeName { get; set; }
public virtual ICollection<Associate> Associates { get; set; }
}
Can't say for sure what is missing from your configuration as you did't post it, but if you are using the fluent api something like this should fix the problem:
modelBuilder.Entity<AssociateType>()
.HasKey(t => t.AssociateTypeId);
modelBuilder.Entity<Associate>()
.HasRequired(t => t.AssociateType)
.WithRequiredPrincipal(t => t.Associate);
The above is adapted from this article http://msdn.microsoft.com/en-us/data/jj591620.aspx

How to create nested JSON response with Entity Framework Query

I want to get a nested JSON reply with my EF query and not sure how to do this.
I have declared my Models as follows:
[Serializable]
public class StockReturnMethod
{
public int WarehouseID { get; set; }
public int ProductSKUID { get; set; }
public int LotID { get; set; }
public string LotName { get; set; }
public int AreaID { get; set; }
public string AreaName { get; set; }
public int BinID { get; set; }
public string BinName { get; set; }
public List<AvailibleStock> Stock { get; set; }
}
[Serializable]
public class AvailibleStock
{
public int WarehouseID { get; set; }
public int ProductSKUID { get; set; }
public string ProductSKUName { get; set; }
public string WarehouseName { get; set; }
public string Status { get; set; }
public int QtyUnassigned { get; set; }
}
Here is my EF query that I have so far.
{
return (from WH in SCMENT.Warehouses
join WL in SCMENT.WarehouseLots on WH.WarehouseID equals WL.WarehouseID
join WA in SCMENT.WarehouseAreas on WL.WarehouseLotID equals WA.WarehouseLotID
join WB in SCMENT.WarehouseBins on WA.WarehouseAreaID equals WB.WarehouseAreaID
join SLI in SCMENT.StockLineItems on WH.WarehouseID equals SLI.WarehouseID
join PSKU in SCMENT.ProductSKUs on SLI.ProductSKUID equals PSKU.ProductSKUID
where SLI.SystemAreaID == 1
select new StockReturnMethod()
{
WarehouseID = WH.WarehouseID,
LotID = WL.WarehouseLotID,
LotName = WL.WarehouseLotName,
AreaID = WA.WarehouseAreaID,
AreaName = WA.WarehouseAreaName,
BinID = WB.WarehouseBinID,
BinName = WB.WarehouseBinName,
ProductSKUID = PSKU.ProductSKUID,
Stock = (Will I create a sub query here?)
}
)
public List<AvailibleStock> Stock { get; set; }
Change this to:
public IList<AvailibleStock> Stock { get; set; }
Edit: Here's working sample from one of my projects that you can use as reference:
public class StatisticsModel
{
public int Id { get; set; }
public int ClientId { get; set; }
public int Opened { get; set; }
public IEnumerable<LocationsModel> Locations { get; set; }
}
public class LocationModel
{
public int Id { get; set; }
public string Name { get; set; }
public int OpenCount { get; set; }
}
return dbSet.Select(x => new StatisticsModel
{
Id = x.Id,
ClientId = x.ClientId,
Opened = x.OpenCount,
Locations = x.Locations.Select(z => new LocationsModel{
Id = z.Id,
Name = z.Store.Name,
OpenCount = z.OpenCount
})
})

Entity Framework Code 1st - Mapping many-to-many with extra info

I've looked through several of the questions here and am not quite connecting all the (mental) dots on this. I would appreciate some help.
My Models (code first):
public class cgArmorial
{
[Key]
[Display(Name = "Armorial ID")]
public Guid ArmorialID { get; set; }
[Display(Name = "User ID")]
public Guid UserId { get; set; }
public string Name { get; set; }
public string DeviceUrl { get; set; }
public string Blazon { get; set; }
public virtual ICollection<cgArmorialAward> ArmorialAwards { get; set; }
}
public class cgArmorialAward
{
public cgArmorial Armorial { get; set; }
public cgAward Award { get; set; }
public DateTime AwardedOn { get; set; }
}
public class cgAward
{
[Key]
[Display(Name = "Award ID")]
public Guid AwardID { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public string Blazon { get; set; }
public virtual ICollection<cgArmorialAward> ArmorialAwards { get; set; }
}
Then in my Context class I have (last 2 entries):
public class Context : DbContext
{
public DbSet<cgUser> Users { get; set; }
public DbSet<cgEvent> Events { get; set; }
public DbSet<cgEventType> EventTypes { get; set; }
public DbSet<cgArmorial> Armorials { get; set; }
public DbSet<cgAward> Awards { get; set; }
public DbSet<cgArmorialAward> ArmorialAwards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<cgUser>()
.HasMany<cgEvent>(e => e.EventAutocrats)
.WithMany(u => u.EventAutocrats)
.Map(m =>
{
m.ToTable("EventAutocrats");
m.MapLeftKey("UserId");
m.MapRightKey("EventId");
});
modelBuilder.Entity<cgUser>()
.HasMany<cgEvent>(e => e.EventStaff)
.WithMany(u => u.EventStaff)
.Map(m =>
{
m.ToTable("EventStaff");
m.MapLeftKey("UserId");
m.MapRightKey("EventId");
});
modelBuilder.Entity<cgArmorialAward>()
.HasRequired(a => a.Armorial)
.WithMany(b => b.ArmorialAwards);
modelBuilder.Entity<cgArmorialAward>()
.HasRequired(a => a.Award)
.WithMany(); // b => b.ArmorialAwards
}
}
I am getting this error when I try to run:
System.Data.Edm.EdmEntityType: : EntityType 'cgArmorialAward' has no
key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ArmorialAwards�
is based on type �cgArmorialAward� that has no keys defined.
Well, as the exception says: You don't have a key defined on your entity cgArmorialAward. Every entity must have a key. Change it to the following:
public class cgArmorialAward
{
[Key, Column(Order = 0)]
[ForeignKey("Armorial")]
public Guid ArmorialID { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Award")]
public Guid AwardID { get; set; }
public cgArmorial Armorial { get; set; }
public cgAward Award { get; set; }
public DateTime AwardedOn { get; set; }
}
The fields in the composite key are foreign keys to the other two tables at the same time, hence the ForeignKey attribute. (I'm not sure if conventions would detect this automatically because you have non-standard names ("cgXXX" for the classes and "XXXId" for the foreign key properties). On the other hand the property names Armorial and Award match the foreign key property names. I'm not sure if EF conventions would consider this. So, perhaps the ForeignKey attribute is not necessary but at least it's not wrong.)