scaffolding seems not to work properly - asp.net-mvc-3

I created a new asp.net MVC3 application (internet application), and then I added a new model with 3 classes:
public class BizCard
{
[Required]
public string BizCardID { get; set; }
[Required]
public string Name { get; set; }
public string Address { get; set; }
public List<string> PhoneNumbers { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public BizType type { get; set; }
public List<BizService> OfferedServices { get; set; }
public string Description { get; set; }
}
public class BizType
{
public int BizTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
}
public class BizService
{
public int BizServiceID { get; set; }
public List<BizType> AllowedBizTypes { get; set; }
public string Name { get; set; }
}
After that, I created a new controller, using the template "Controller with read/write actions and views using entity framework", I set the Model class to be "BizCard" and the data context class to be a new class which is called "BizDB". I was expecting to get a new class named BizDB that inherits from DbContext and includes 3 instances of DbSet:
DbSet<BizCard>, DbSet<BizType>, DbSet<BizService>.
In spite of that, I get the class with only one:
DbSet<BizCard>.
Am I missing something?

You are doing this using EF Code First approach.
1. So, you have to create a context class which should inherit DbContext containing required models as DbSet
2. Build the solution. Otherwise it will not be displayed at controller creation
Then you can create the controller using necessary model and its dbcontext.

Related

Entity History is not working in aspnetboilerplate

I am using aspnetboilerplate and added below configuration in preintiliaze in module. I have also added data annotation Audited to my entity but still it is not working. My entity is inheriting from AuditedEntity as don't need deleted feature. Please help
Configuration.EntityHistory.IsEnabled = true; Configuration.EntityHistory.Selectors.Add(new NamedTypeSelector("Abp.AuditedEntities", type => typeof(AuditedEntity).IsAssignableFrom(type)));
I have taken reference from here Can't enable Entity History in ASP.NET Zero
Below is entity definition
[Audited]
public partial class QuestionResponse : AuditedEntity<long>
{
public long ApplicationId { get; set; }
public long QuestionId { get; set; }
public string Response { get; set; }
public string Remark { get; set; }
public bool IsActive { get; set; }
public Application Application { get; set; }
public AbpUsers CreatorUser { get; set; }
public AbpUsers LastModifierUser { get; set; }
public Question Question { get; set; }
}
AuditedEntity<long> is not assignable to AuditedEntity.
Add a selector based on the interface IAuditedEntity instead.
Configuration.EntityHistory.Selectors.Add(
new NamedTypeSelector("Abp.AuditedEntities", type =>
// typeof(AuditedEntity).IsAssignableFrom(type)));
typeof(IAuditedEntity).IsAssignableFrom(type)));
Reference
From aspnetboilerplate/aspnetboilerplate's AuditedEntity.cs:
public abstract class AuditedEntity : AuditedEntity<int>, IEntity
{
}
public abstract class AuditedEntity<TPrimaryKey> : CreationAuditedEntity<TPrimaryKey>, IAudited
{
...
}

Map a complex MVC ViewModel in knockout

I have a MVC view which contains observables and observablearrays. What is the proper way to map?
My MVC ViewModel looks similar to this:
public class CategorysEditViewModel
{
public Category Categorys { get; set; }
public IEnumerable<Expert> candidates { get; set; }
public IEnumerable<Expert> experts { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Policy { get; set; }
}
public class Expert
{
public int ExpertID { get; set; }
public int CategoryID { get; set; }
public string Name { get; set; }
public string ExpertGUID { get; set; }
}
Assuming the cshtml file is strongly typed to the CategorysEditViewModel, you can serialize the view model object to JSON and then use the ko.mapping tool to map it to a knockout viewmodel.
Check out this post: Best practice on passing Mvc Model to KnockoutJS

Entity Framework POCO Relationships

I am trying to implement code-first approach of entity framework. I have four entities UserInfo, Client, Admin and Account. I want relationships as:
Each Client has a UserInfo
Each Admin has a `UserInfo
Each Account is linked with a User(UserInfo)
Assuming these things i wrote the POCO models. With the relationships i want, is it correct ?Am i missing something?
public class UserInfo
{
public int UserInfoID { get; set; }
public Name Name { get; set; }
public Address Address { get; set; }
public Contact Contact { get; set; }
}
public class Admin
{
public int AdminID { get; set; }
public int UserInfoID { get; set; }
[ForeignKey("UserInfoID")]
public virtual UserInfo UserInfo { get; set; }
}
public class Client
{
public int ClientID { get; set; }
public CompanyDetails CompanyDetails { get; set; }
public int UserInfoID { get; set; }
[ForeignKey("UserInfoID")]
public virtual UserInfo UserInfo { get; set; }
}
public class Account
{
public int AccountID { get; set; }
[Required, Column("Balance"), Display(Name = "Account Balance")]
public double Balance { get; set; }
public int UserInfoID { get; set; }
[ForeignKey("UserInfoID")]
public virtual UserInfo UserInfo { get; set; }
}
What you have appears to be correct based on your requirements however I personally prefer the Entity Framework Model Builder when configuring your entities with Code First.
Using the model builder means that you don't have any attributes on your POCO entities which in turn means that you don't need an EF reference to use the entities.
Take a look at my article here for some more info on how to use the modelbuilder : http://blog.staticvoid.co.nz/2012/07/entity-framework-navigation-property.html

What is the proper sequence of method calls when using a multi layered architecture?

I have built a simple survey-tool using MVC 3 with only 1 layer (MVC). I regret this now. All my database access and mapping is handled in the controllers, and some other mapping classes.
I would like to switch over to using three layers:
Presentation (MVC)
Business Logic
Data / Persistence (EF)
I am using the Entity Framework to handle everything with the database. The entity framework creates it's own domain classes. Where should the mapping between the Models that MVC uses and the models that EF creates go?
If the mapping is in the business layer, is there a need for the Models folder in the MVC project?
A survey-question consists of the Question itself, Rows and Columns. Theese are the models that i use:
public class Question {
public int Question_ID { get; set; }
public Boolean Condition_Fullfilled;
[Required(ErrorMessage = "Dette felt er påkrævet")]
public String Question_Wording { get; set; }
public String Question_Type { get; set; }
[Required(ErrorMessage = "Dette felt er påkrævet")]
public String Question_Order { get; set; }
public String Left_scale { get; set; }
public String Right_scale { get; set; }
public int Scale_Length { get; set; }
public String Left_Scale_HelpText { get; set; }
public String Right_Scale_HelpText { get; set; }
public Boolean Visible { get; set; }
public Boolean IsAnswered { get; set; }
public String Question_HelpText { get; set; }
public int Category_ID { get; set; }
}
public class MatrixColumns
{
public int Column_ID { get; set; }
public int Column_Number { get; set; }
public String Column_Description { get; set; }
public Boolean IsAnswer { get; set; }
public int? Procent { get; set; }
public bool Delete { get; set; }
public bool Visible { get; set; }
public int? Numbers { get; set; }
public String Help_Text { get; set; }
}
public class MatrixRows
{
public bool Delete { get; set; }
public bool Visible { get; set; }
public int Row_Id { get; set; }
public String Row_Number { get; set; }
public String Row_Description { get; set; }
public String Special_Row_CSS { get; set; }
public String Help_Text { get; set; }
// Dette er summen af procenterne af alle kolonner i rækken
public int RowSum { get; set; }
}
All the data for theese models is retrieved in the Controller, based upon a QuestionID, and mapped to a ViewModel that looks like this:
public class ShowMatrixQuestionViewModel : Question
{
public Dictionary<MatrixRows, List<MatrixColumns>> columnrow { get; set; }
public List<MatrixColumns> columns { get; set; }
public List<MatrixRows> rows { get; set; }
public ShowMatrixQuestionViewModel()
{
columns = new List<MatrixColumns>();
rows = new List<MatrixRows>();
columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
}
}
So when i want to send a ShowMatrixQuestionViewModel to a View from my Controller, what is the route i should take?
This is my suggestion:
-> Controller calls a method in the business layer called
public ShowMatrixViewModel GetQuestion(int QuestionID) {}
-> GetQuestion calls the following methods in the data layer:
public Question GetQuestion(int QuestionId) {}
public MatrixRows GetRows(int QuestionId) {}
public MatrixColumns GetColumns(int id) {}
-> Entity framework returns "pure" objects, which i want to map over to the ones i posted above
-> GetQuestion calls methods to map the EF models to my own models
-> Last GetQuestion calls a method that maps the Questions, Rows and Columns:
ShowMatrixQuestionViewModel model = MapShowMatrixQuestionViewModel(Question, MatrixRows, MatrixColumns)
return model;
Is this correct?
Thanks in advance
To answer the first part of your question:
"Where should the mapping between the Models that MVC uses and the models that EF creates go?"
The answer is that the models MVC uses are the models created by the EF. Your EF tool in the ASP.NET MVC project is either Linq to SQL Classes or the ADO.NET Entity Framework Model. You should create these inside the Models folder in your project and they provide your data / persistence (EF).

Mvc3 listboxfor helper method selected items when editing an entity

I am working on a asp.net mvc3 demo app with razor that is hosted on http://portfolio-6.apphb.com/Projects
I have mvc scaffolding, Poco objects as entities and mvc scaffolding running.
public class Project
{
public int ProjectId { get; set; }
[Required(ErrorMessage="please enter name")]
public string Name { get; set; }
public string Url { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public bool isFeatured { get; set; }
public bool isDisabled { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
[Required(ErrorMessage="Please select atleast one tag")]
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
public class Group
{
public int GroupId { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
First Problem
From above code you can see I have many to many relation between Project and Tag entities.
Now I have mange to implement New project feature with one bug that is Tags property of Project marked as required but client site validation is not working if you don't select any tag.
Second Problem
Once a project is created with mutliple tags if you decide to edit it how to show the tags selected that are associated with the project???
You can find complete source code at https://github.com/najamsk/PortfolioManger
I have manage to make validation work and tags are now getting selected while editing a project.
But now I run into another problem lets say I create a new project with tag "asp.net" and later I edit project and choose one tag only "wordpress". Now if i post form model binding works but my context cant update tags collection property of the project? It seems to me it entity framwork making problem here when it finds a complex type property for my Project entity?
public void InsertOrUpdate(Project project)
{
if (project.ProjectId == default(int)) {
// New entity
foreach (var tag in project.Tags)
{
context.Entry(tag).State = EntityState.Unchanged;
}
context.Projects.Add(project);
}
else {
// Existing entity
//every property of project entity is
//getting updated only tags property left out :(
context.Entry(project).State = EntityState.Modified;
}
}

Resources