public class Denial
{
[Key]
public string DenialCd { get; set; }
[Required]
public string DenialDesc { get; set; }
public ICollection<CaseDenial> CaseDenials { get; set; }
}
public class CaseDenial
{
[Key]
public int ID { get; set; }
[Required]
public string DenialCd { get; set; }
[Required]
public int CaseId { get; set; }
[ForeignKey("DenialCd")]
public Denial Denial { get; set; }
[ForeignKey("CaseId")]
public Case Case { get; set; }
}
var query = EntityQuery.from('CaseDenials')
.where("CaseId", "==", caseID)
.expand("Denial")
.orderBy("DenialCd").inlineCount();
CaseDenial table links to DenialCd column of Denial table. Above breeze query brings null for the navigation property "Denial" while fetching records from CaseDenials.
My guess is that your EF model is not attributed 'correctly'. I would confirm that you can perform a server side Entity Framework 'Include' operation. This is what Breeze is doing under the covers when you call a client side 'expand'.
As far as I know, if you have 1 denial with MANY case denials, then you should query as follows:
var query = EntityQuery.from('Denials')
.where("CaseDenial.CaseId", "==", caseID)
.expand("CaseDenial")
.orderBy("DenialCd").inlineCount();
This should work. What you can try as well is adding [InverseProperty("CaseDenials")]
Related
my db looks like this:
public class BrContext:DbContext
{
public DbSet<Conversation> AllConversations { get; set; }
public DbSet<ChatReference> ChatReferences { get; set; }
public DbSet<Product> Products { get; set; }
}
My Conversation Model looks like this:
public class Conversation
{
[Key]
public int ConversationId { get; set; }
public string ConverserName { get; set; }
public List<ChatReference> AllReferences { get; set; }
public ChatReference CurrentChatReference { get; set; }
public bool IsDealtWith { get; set; }
}
My ChatReference Model looks like this:
public class ChatReference
{
public int ChatReferenceId { get; set; }
public string ChatReferenceTime { get; set; }
public string ChatReferenceContent { get; set; }
public bool IsR { get; set; }
}
as you see - I have a list {"AllReferences"} of 'CurrentChatReference' as a property in a model that is saved in the DB.
I have times in the course of debugging the project , that when i look at the values in the DB - i see that the order of the ChatReferences in the list 'AllReferences' in the latest conversation in the db has been switched around.
Does anyone have an idea of why this is happening?
When backend query is returning the results, those results are populated in the List. If you are not sending the query using OrderBy, the results are not guaranteed to always come in the same order, so the items in the List are not always in the same order. Either you retrieve the results using OrderBy clause or Sort the results after you populate the results into your model.
Entity Framework Ordering Includes
How to Sort a List<T> by a property in the object
I have a model that looks like this
public abstract class Item
{
public int ItemId { get; set; }
public String Title { get; set; }
public String Description { get; set; }
public DateTime PurchaseDate { get; set; }
public ICollection<String> Pictures { get; set; }
public Int32 MinimumPrice { get; set; }
public DateTime Deadline { get; set; }
public Boolean VisibleBids { get; set; }
public Boolean Handled { get; set; }
public DateTime PlacementDate { get; set; }
public ApplicationUser User { get; set; }
}
In my controller I do
db.Items.ToList()
This leaves the Pictures field for all fetched objects null because its how the entity framework works. What is a good solution to fetch them in one query?
I hope you already done with Navigation properties between your tables, Now you just need to make your collection virtual and use the concept of eager loading when you need data from both the tables
public virtual ICollection<String> Pictures { get; set; }
and use include in linq like
db.Items.Include("Pictures").ToList()
So here by making virtual navigation you are saying that only take the data of related entity when you needed and whenever you need the data use the Include for eager loading.
For setting navigation properties please have a look on the code.
Suppose the scenario where we have a Post and on this we have multiple comments like
class Posts
{
public int PostsId { get; set; }
public string PostsDescription { get; set; }
public virtual ICollection<Comments> Comments { get; set; }
}
class Comments
{
public int CommentsId { get; set; }
public string CommentsDescription { get; set; }
public int PostsId { get; set; }
public virtual Posts Posts { get; set; }
}
I encounter a problem with deserializing entities in client side with breeze.js.
My data model entity has one to many relation to other entities (A has ICollection of B)
When I make a query , I see the data returned from the server include $ref= # , I understood breeze uses this to identify same objects returned from the server.
But in client side all those entities with $ref=# doesn't deserialized properly I get this function () { return mc.refMap[node]} insted to get the real object in client side .
Here is my object structure :
public partial class Product
{
public Product()
{
this.ProductCatalogue = new HashSet<ProductCatalogue>();
this.DiameterRanges = new HashSet<DiameterRanges>();
this.Product_Children = new HashSet<Product>();
}
public int Id { get; set; }
public string Code { get; set; }
public virtual ICollection<ProductCatalogue> ProductCatalogue { get; set; }
public virtual ICollection<DiameterRanges> DiameterRanges { get; set; }
public virtual Product Product_Parent { get; set; }
}
public partial class DiameterRanges
{
public int Id { get; set; }
public double MinSPH { get; set; }
public double MaxSPH { get; set; }
public double MinCyl { get; set; }
public double MaxCyl { get; set; }
public short MinDiameter { get; set; }
public short MaxDiameter { get; set; }
public int Product_Id { get; set; }
public virtual Product Product { get; set; }
}
Nothing special in my server side query : Context.Product.Include("DiameterRanges");
Any idea to figure out this problem .
Thanks in advance ....
Ok so I'm adding on to the Simplemembership.
Model UsersProfiles
namespace OilNGasWeb.Models
{
[Table("Users")]
public class UserProfiles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Initials { get; set; }
public string Company { get; set; }
public string Department { get; set; }
public string Title { get; set; }
public string Team { get; set; }
public string TeamSub { get; set; }
public string Phone { get; set; }
public string ImageLocation { get; set; }
public string CurrentlyAuthorized { get; set; }
public string Note { get; set; }
public string Comment { get; set; }
//public virtual dbClient Client { get; set; }
public virtual ICollection<Roles> Roles { get; set; } //many to many
public virtual ICollection<dbClient> Clients { get; set; } // many to many
}
}
Roles
namespace OilNGasWeb.Models
{
[Table("webpages_Roles")]
public class Roles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Required(ErrorMessage = "Required")]
public int RoleID { get; set; }
[Required(ErrorMessage = "Required")]
public string RoleName { get; set; }
public virtual ICollection<UserProfiles> UserProfiles { get; set; } //many to many
}
}
My issue now that i have it creating the many to many tables like i saw it creat before modifications my question is how to get those tables Renamed
webpages_UsersInRoles
I would prefer not to go into SSMS and change them physically rather tell MVC to use a different instance
From the code above EF produced RolesUserProfiles instead of webpages_UsersInRoles
The error shows when the program is trying to #if (User.IsInRole("Administrator")) validade user.
Naturally I hit F12 on IsInRole to bring me to the definition....
it does but there all empty
Now what ? how can i recode if its hidden from me ? where is the code at , and how can i Modify this?
What i would like out of all this is
either renaming the tables ManytoMany as they are being created
being able to modify the code that looks for webpages_UsersInRoles
Thanks in advance.
You cannot rename the tables. The table names are hard coded in SimpleMembership. You can see the source code here:
http://aspnetwebstack.codeplex.com/SourceControl/latest#src/WebMatrix.WebData/SimpleMembershipProvider.cs
Don't use the EF navigational properties. You should be accessing this information via the Membership or WebSecurity API's.
If you really want to do this, then you will need to configure EF to use the tablenames required by simple membership, which means utilizing the fluent mapping syntax.. which is not exactly intuitive.
I am working with the EF Code First library trying to work on an appointment scheduling app.
The model's I have are going to be a Client, Appointment, and AppointmentType...
Basically each Client can have a set of Appointments, and each Appointment can have a single AppointmentType...
The code is as follows:
public class Client
{
[ScaffoldColumn(false)]
public int ClientID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[EmailAddress]
[Required]
public string Email { get; set; }
[DataType("DateTime")]
public DateTime Birthday { get; set; }
[Required]
public string CellPhone { get; set; }
public string HomePhone { get; set; }
public string Notes { get; set; }
public virtual ICollection<Appointment> Appointments{ get; set; }
public string Name {
get{
return FirstName + " " + LastName;
}
}
public class Appointment
{
[ScaffoldColumn(false)]
public int AppointmentID { get; set; }
[ScaffoldColumn(false)]
public int ClientID { get; set; }
[ScaffoldColumn(false)]
public int AppointmentTypeID { get; set; }
[Required]
public DateTime AppointmentDate { get; set; }
public string Notes { get; set; }
public virtual AppointmentType AppointmentType { get; set; }
public virtual Client Client { get; set; }
}
public class AppointmentType
{
[ScaffoldColumn(false)]
public int AppointmentTypeID { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public virtual Appointment Appointment { get; set; }
}
Everything works well when I create an appointment type, and a client, but when I create an appointment I get the following error...
InnerException {"The INSERT statement conflicted with the FOREIGN KEY constraint \"Appointment_Client\". The conflict occurred in database \"Salon.Models.SalonContext\", table \"dbo.Clients\", column 'ClientID'.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
If more details are needed, please let me know...I am just trying to figure out if I am missing anything in the setup.
This is what happens when I debug on the post to create the Appointment...All the ID's are as 0 which is correct, but should the other fields not be null?? Or does it matter...Just not very familiar with how things should look this being my first EF Code First project...
According to your setup, one AppointmentType can only have one Appointment. This is a one-to-one mapping. In this case, you better move the AppointmentType into the Appointment entity. Otherwise, what I believe is more logical, an AppoitmentType can have many Appointments but one Appointment can have only one AppointmentType. Accordingly, you should have a virtual ICollection inside your AppointmentType entity.
public class AppointmentType
{
[ScaffoldColumn(false)]
public int AppointmentTypeID { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
}
I am not sure this is what's causing the problem but it could be. Sometimes mapping faults cause some weird exceptions to be thrown. Give it a try and let me know if your problem gets resolved.
By your constraints AppointmentType and Client cannot be null in Appointment. You can delete constraints or set correct objects in object properties. For example create Client and AppointmentType and then create Appointment for created Client with created AppointmentType