how to query navigation properties code-first MVC4 - sql

What is wrong here? I have a many to many between products and transactions. Trying to build the view but I can't get any intellisense for the query to load the ProductTransaction table.
entity 1
[Table("Transactions")]
public class Transaction
{
[Key]
public virtual int TID { get; set; }
public virtual int FromUserID { get; set; }
public virtual int ToUserId { get; set; }
public virtual int itemForId { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
entity 2
[Table("Products")]
public class Product
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public virtual int ProductId { get; set; }
[Required]
[StringLength(50, ErrorMessage="Name can't be longer than 50 characters bitch")]
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual DateTime? UploadDate { get; set; }
public virtual byte[] ProductImage { get; set; }
[StringLength(25)]
public virtual string MimeType { get; set; }
public virtual int Views { get; set; }
public virtual int Qty { get; set; }
// Relations
public virtual UserProfile User { get; set; }
public virtual int UserId { get; set; }
public virtual int CategoryId { get; set; }
public virtual ICollection<Bag> Bags { get; set; }
public virtual ICollection<Transaction> Transactions{ get; set; }
}
controller method to see all transactions and trades: query has no intellisense for Products
public ViewResult GetTrades()
{
//Include is not recognizing any nav properties
var friend = db.Transactions.Include(a => a.Products);
}
many to many relationship
modelBuilder.Entity<Transaction>()
.HasMany(p => p.Products)
.WithMany(t => t.Transactions)
.Map(m =>
{
m.ToTable("ProductTransactions");
m.MapLeftKey("ProductId");
m.MapRightKey("TID");
});

DbExtensions.Include(this IQueryable, Expression<Func<T, TProp>>) is an extension method.
You need to include the namespace to access it as an extension method.
using System.Data.Entity;

Related

CollectionNavigationBuilder does not contain a definition for .WithMany

I am using .NET 3.1.21 and I'm getting the error:
'CollectionNavigationBuilder<ApplicationUser, ClassroomGroup>' does not contain a definition for 'WithMany' and no accessible extension method 'WithMany' accepting a first argument of type 'CollectionNavigationBuilder<ApplicationUser, ClassroomGroup>' could be found (are you missing a using directive or an assembly reference?)
The syntax seems to be okay from what I've seen so far.
ApplicationDbContext
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
relationship.DeleteBehavior = DeleteBehavior.Restrict;
builder.Entity<ApplicationUser>().HasMany(x => x.TeacherGroups).WithOne(x => x.Teacher).OnDelete(DeleteBehavior.SetNull);
builder.Entity<ApplicationUser>().HasMany(x => x.StudentGroups).WithMany(x => x.Students); //this .WithMany gives me the error
builder.Entity<ApplicationUser>().HasMany(x => x.UserRoles).WithOne(x => x.User)
.HasForeignKey(x => x.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
}
ApplicationUser
public class ApplicationUser : IdentityUser
{
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
public virtual ICollection<ClassroomGroup> TeacherGroups { get; set; }
public virtual ICollection<ClassroomGroup> StudentGroups { get; set; }
public virtual ICollection<Task> CreatedTasks { get; set; }
public virtual ICollection<SchoolClassStudentTerm> SchoolClassStudentTerms { get; set; }
public virtual ICollection<GroupAssignment> CreatedGroupAssignments { get; set; }
public bool Disabled { get; set; }
public String FullName { get; set; }
public DateTime? StartTime { get; set; }
}
ClassroomGroup
public class ClassroomGroup
{
public int SubjectId { get; set; }
public virtual Subject Subject { get; set; }
public virtual SchoolClass SchoolClass { get; set; }
public int SchoolClassId { get; set; }
public String TeacherId { get; set; }
public virtual ApplicationUser Teacher { get; set; }
public int Id { get; set; }
public String Title { get; set; }
public virtual ICollection<ApplicationUser> Students { get; set; }
public int TermId { get; set; }
public virtual Term Term { get; set; }
public virtual ICollection<GroupAssignment> GroupAssignments { get; set; }
}
All packages are up to date

EF Code First not setting foreign key in one to one relationship

So I'm trying to create a simple Product-Preview 1 to 1 relationship as follows:
public class Product : BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string name { get; set; }
public virtual EPS eps { get; set; }
public virtual Preview preview { get; set; }
[ForeignKey("userId")]
public virtual User user { get; set; }
public Guid userId { get; set; }
}
and
public class Preview : BaseEntity
{
[Key,ForeignKey("Product")]
public Guid Id { get; set; }
public string imagePath { get; set; }
public double width { get; set; }
public double height { get; set; }
public virtual List<TextPreview> Texts { get; set; }
public virtual List<ImagePlaceHolder> ImagePlaceHolders { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
public virtual Guid ProductId { get; set; }
}
I was expecting to have a foreign key in the Previews table that would point to a Product
but after running the migration I just get it as regular field
What I'm I doing wrong?
You almost have it you just missed one piece of the puzzle ...
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
You also need to add ...
public Guid ProductId { get; set; }
to the preview object.
It's also worth noting that the ForeignKey attrib can be put on either property, and the string must refer to the other one of the pair.
As it's currently written you are trying to make the Id property specify the value for both the primary key and the foreign key on the tables in question.
So your final code might look something like ...
public class Product : BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[ForeignKey("User")]
public Guid UserId { get; set; }
public string name { get; set; }
public virtual EPS eps { get; set; }
public virtual Preview preview { get; set; }
public virtual User user { get; set; }
}
and
public class Preview : BaseEntity
{
[Key]
public Guid Id { get; set; }
[ForeignKey("Product")]
public Guid ProductId { get; set; }
public string imagePath { get; set; }
public double width { get; set; }
public double height { get; set; }
public virtual List<TextPreview> Texts { get; set; }
public virtual List<ImagePlaceHolder> ImagePlaceHolders { get; set; }
public virtual Product Product { get; set; }
}
As a side note I would also recommend against using concrete collection types like List<T> instead use something like IList<T> or ICollection<T> it promotes better code reuse and extensibility.

Fluent NHibernate mapping throws an exception of 'Id is not mapped'

I need help with auto mapping in Fluent Nhibernate. Here's the tables I want to have in my app (they are many of them, but I want to start from mapping only a few of them)
Well, I'd like to use the AutoMapping functionality because I don't want to write the mapping classes for more than 100 tables...
Anyway, here's the error thrown when creating the SessionFactory (the code is at the end of this post)
The entity 'FilterConfig' doesn't have an Id mapped.
Use the Id method to map your identity property. For example: Id(x => x.Id).
Entities (I hope I created them correctly):
public partial class UserLogin
{
public UserLogin()
{
this.UserMessages = new List<UserMessage>();
this.UserMessagesReceivers = new List<UserMessagesReceiver>();
}
public virtual int ID { get; set; }
public virtual int UserTypeID { get; set; }
public virtual int? StudentID { get; set; }
public virtual int? HeadmasterID { get; set; }
public virtual int? ParentID { get; set; }
public virtual string UniqueID { get; set; }
public virtual bool ShowMyPhoneNumber { get; set; }
public virtual bool IsBanned { get; set; }
public virtual string Login { get; set; }
public virtual string Password { get; set; }
public virtual bool WasPasswordSent { get; set; }
public virtual string Email { get; set; }
public virtual string UserPicture { get; set; }
public virtual IList<UserMessage> UserMessages { get; set; }
public virtual IList<UserMessagesReceiver> UserMessagesReceivers { get; set; }
}
public partial class UserMessage
{
public UserMessage()
{
this.UserMessagesReceivers = new List<UserMessagesReceiver>();
this.UserMessagesReplies = new List<UserMessagesReply>();
}
public virtual int ID { get; set; }
public virtual DateTime Date { get; set; }
public virtual DateTime? LastCheckDate { get; set; }
public virtual int UserLoginID { get; set; }
public virtual string Description { get; set; }
public virtual bool HasNonCheckedReplies { get; set; }
public virtual UserLogin UserLogin { get; set; }
public virtual IList<UserMessagesReceiver> UserMessagesReceivers { get; set; }
public virtual IList<UserMessagesReply> UserMessagesReplies { get; set; }
}
public partial class UserMessagesReceiver
{
public UserMessagesReceiver()
{
this.WasMessageChecked = false;
this.UserMessagesReplies = new List<UserMessagesReply>();
}
public virtual int ID { get; set; }
public virtual int UserMessagesID { get; set; }
public virtual int ReceiverLoginID { get; set; }
public virtual bool WasMessageChecked { get; set; }
public virtual DateTime? LastCheckedDate { get; set; }
public virtual UserLogin UserLogin { get; set; }
public virtual UserMessage UserMessage { get; set; }
public virtual IList<UserMessagesReply> UserMessagesReplies { get; set; }
}
public partial class UserMessagesReply
{
public UserMessagesReply()
{
}
public virtual int ID { get; set; }
public virtual DateTime Date { get; set; }
public virtual int UserMessagesID { get; set; }
public virtual int? UserMessagesReceiverID { get; set; }
public virtual string Description { get; set; }
public virtual UserMessage UserMessage { get; set; }
public virtual UserMessagesReceiver UserMessagesReceiver { get; set; }
}
Configuration:
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool IsId(Member member)
{
return member.Name == member.DeclaringType.Name + "ID";
}
}
private static AutoPersistenceModel CreateAutomappings()
{
return AutoMap.AssemblyOf<AutomappingConfiguration>(new AutomappingConfiguration());
}
private static ISessionFactory CreateSessionFactory()
{
var cfg = new AutomappingConfiguration();
return Fluently.Configure()
.Database(MySQLConfiguration.Standard
.ConnectionString("..."))
.Mappings(m => m.AutoMappings
.Add(AutoMap.AssemblyOf<UserLogin>(cfg))
.Add(AutoMap.AssemblyOf<UserMessage>(cfg))
.Add(AutoMap.AssemblyOf<UserMessagesReceiver>(cfg))
.Add(AutoMap.AssemblyOf<UserMessagesReply>(cfg)))
.BuildSessionFactory();
}
in the configuration you said that the Ids are named like UserLoginID but in the class they are defined as public virtual int ID { get; set; } so change
return member.Name == member.DeclaringType.Name + "ID";
to
return member.Name == "ID";
Some additional info:
CreateAutomappings() seems to be not used
AutomappingConfiguration should at least override ShouldMap(Member) to filter on the namespace (e.g. member.Namespace.StartsWith(typeof(UserMessage).Namespace)) otherwise sooner or later utility classes and the like will be mapped as well
AutoMap.AssemblyOf<> should be called per assembly containing types not per type

How to create a history model of a specific model on MVC 4

I'm still new in creating Models using Entity Framework and MVC 4 Razor. I'm having a problem on how can I save a history of a model. How can I create a model that have a history on specific tables or fields ? For ex: If I wish to create a history on the changes on the school. Its still not clear to me how will I I create the model that saves history. How will be the triggering do I have to execute the save function on different models with the same data ?
Thank you so much in advance.
If anyone could be a simple example of model and a model history and how it is functioning, I'll be very grateful. Like a Sales or sales history.
Here's my code
One To Many
public class Child
{
[Key]
public int ChildID { get; set; }
[Required,Display(Name="Project Code")]
public string ProjectCode { get; set; }
public string Status { get; set; }
[DataType(DataType.Date)]
public DateTime StatusDate { get; set; }
public string FamilyName { get; set; }
public string GivenName { get; set; }
public string MiddleName { get; set; }
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }
public string Gender {get;set;}
public string Address { get; set; }
public string Section { get; set; }
public int SchoolLevelID { get; set; }
public int SchoolYearID { get; set; }
public int AreaID { get; set; }
public int SchoolID { get; set; }
public int GradeLevelID { get; set; }
//Foreign Key - One to Many
public virtual SchoolLevel SchoolLevel { get; set; }
public virtual SchoolYear SchoolYear { get; set; }
public virtual Area Area { get; set; }
public virtual School School { get; set; }
public virtual GradeLevel GradeLevel{get;set;}
//Child is foreign key at the table
public virtual ICollection<Guardian> Guardians { get; set; }
}
public class SchoolLevel
{
public int SchoolLevelID { get; set; }
public string SchoolLevelName { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class SchoolYear
{
public int SchoolYearID { get; set; }
public string SchoolYearName { get; set; }
public virtual ICollection<Child> Children{get;set;}
}
public class Area
{
public int AreaID{get;set;}
public string AreaName { get; set; }
public virtual ICollection<Child> Children{get;set;}
}
public class School
{
public int SchoolID { get; set; }
public string SchoolName{get;set;}
public virtual ICollection<Child> Children { get; set; }
}
public class GradeLevel
{
public int GradeLevelID{get;set;}
public string GradeLevelName { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class ChildDbContext : DbContext
{
public DbSet<Child> Children { get; set; }
public DbSet<SchoolLevel> SchoolLevels { get; set; }
public DbSet<SchoolYear> SchoolYears { get; set; }
public DbSet<Area> Areas { get; set; }
public DbSet<School> Schools { get; set; }
public DbSet<GradeLevel> GradeLevels { get; set; }
public DbSet<Guardian> Guardians { get; set; }
}
You can use this approach: Create a History model. That contains 1 changeness like o log.
public class History
{
public int HistoryId { get; set; }
public int ModelType { get; set; } //it is ModelTypeEnum value.
public int ModelId { get; set; }
public string PropertyName { get; set; }
public string Propertyvalue {get;set;}
public DateTime ChangeDate { get; set; }
public int ChangedUserId { get; set; }
}
And Enum:
public enum ModelTypeEnum
{
Child =1,
SchoolLevel = 2,
//etc..
};
For example, when you edit 1 Child entity, give changed properties name and value, it's id, type and others (ChangeDate, ChangedUserId) to History and save histories. If 3 properties will change you should save 3 history entities. Then, you can load (filter) histories by ModelId, by ChangedUserId etc.

NHibernate Left Outer Join SubClass

I have 2 entities products and images. Not all images are product images, and images are a sub class of a file below are my entities. I need to find all of the images that are not associated to a product. I'm new to retrieval of entities and have tried numerous approaches. Any ideas or links would be greatly appreciated.
public class File
{
#region Feilds
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Enumerations.File.FileType Type { get; set; }
public virtual string Extension { get; set; }
public virtual string Path { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual DateTime DateModified { get; set; }
#endregion
}
public class Image : File
{
#region Fields
public virtual string ImageName { get; set; }
public virtual string Description { get; set; }
public virtual bool Active { get; set; }
public virtual DateTime DateTaken { get; set; }
#endregion
}
public class Product
{
#region Properties
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual decimal Price { get; set; }
public virtual decimal Weight { get; set; }
public virtual bool IsDigital { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual IList<Category> ProductCategories { get; set; }
public virtual IList<ProductAttribute> ProductAttributes { get; set; }
public virtual IList<Image> ProductImages { get; set; }
#endregion
}
You can use a Critiria not exists subquery...
IList<Image> images = session.CreateCriteria<Image>("img")
.Add(Expression.Not(Subqueries.Exists(DetachedCriteria.For<ProductImageLink>("pil")
.SetProjection(Projections.Constant(1))
.Add(Expression.EqProperty("img.image_id", "pil.image_id")))))
.List<Image>();
Where ProductImageLink is the association table.
Should result in a query like...
select ... from image img where not exists(select 1 from productimagelink pil where img.image_id = pil.image_id);