MVC Entity Framework Ignore Class/Model - asp.net-mvc

I am currently building an MVC site using Entity Framework. I have created following class:
using System; using System.Collections.Generic;
using System.Linq; using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace .Models {
public class VehicleTableData
{
[NotMapped]
public Dictionary<string,string> StandardVehicleData { get; set; }
[NotMapped]
public Dictionary<string,string> AdditionalData { get; set; }
} }
However, I would like it to be ignored by Entity Framework as when I try to create a view with it I get the error that there is no valid key.

If you have the class defined in your DbContext with the following line of code:
public DbSet<VehicleTableData> VehicleTableDatas { get; set; }
This will cause Entity Framework to include the class. Once the above line is removed it will not be included.
You could also remove the [NotMapped] attributes as this would only apply to properties that you would not want saved to the database in a model included in your DbContext.

Related

Adding Table to Database with Migrations

I made some changes to my models, these included adding a new field to a class and creating a new class (table), as shown below:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace SurveyTool.Models
{
public class Industry
{
[Key]
public int ID { get; set; }
public string name { get; set; }
}
}
When I Add-Migration and Update-Database, the new field was added. However, the new table wasn't. I tried the process again and the newest migration is empty.
Why isn't the standard process for adding a new table working?
Thanks!

How Entity Framework know to which class is take to add migration?

My question is very simple, I am using Entity Framework with asp.net MVC.
I am not sure this question is helpful or not, but I have a doubt.
How entity framework should known, that he has to add migration for which class, why EF will not generate migration for ViewModel class ?
I am just want to know how EF will differentiated between ViewModel.cs class and Model.cs class and add migration only for model.
Thanks in advance.
I think it checks which classes have been added to a DbContext.
using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Infrastructure;
namespace MigrationsDemo
{
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
}
}
Blog will now be considered with the migration. Please see this link for more information on code-first migrations: https://msdn.microsoft.com/en-us/data/jj591621.aspx
There are some places that you can add configuration to Entity Framework, but in its basic form, it iterates the properties on your DbContext class (with a public get), and considers all properties of type IDbSet to be a part of the data model.
The properties do not have to have a set. For example, this is a valid entity definition in DbContext that EF will pick up and generate Migration for:
public IDbSet<MyEntity> MyEntities
{
get { return Set<MyEntity>(); }
}

Creating Controllers in MVC 5 using the Models present in a Class Library

I have a bit of a problem. I have created all my models in a different Class Library. Now, I have to create a controller in a MVC Web Application using the models present in the Class Library. I am not able to see the Models that I created in the Class Library while creating a controller in MVC. How do I proceed on getting all the models in Class Library to the MVC project. I have even added a reference of the Class Library to the MVC project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainClasses;
namespace TestingModel.Controllers
{
public class ProductsGroupController : Controller
{ //
// GET: /ProductsGroup/
public ActionResult Index()
{
return View();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web;
namespace DomainClasses.Models
{
public class ProductGroupMaster
{
public int ProductGroupID { get; set; }
}
}

creating controller doesn't work

I'm learning asp.net mvc3 from w3schools and following that tutorial.http://w3schools.com/aspnet/mvc_models.asp In the section "ASP.NET MVC Models" I have created the model like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcDemo.Models
{
public class MovieDB
{
public int ID { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public DateTime Date { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<MovieDB> Movies { get; set; }
}
}
Then I was going to add a controller according to the instructions.
In the Solution Explorer, right-click the Controllers folder, and select Add and Controller
Set controller name to MoviesController
Select template: Controller with read/write actions and views, using Entity Framework
Select model class: MovieDB (McvDemo.Models)
Select data context class: MovieDBContext (McvDemo.Models)*
Select views Razor (CSHTML)
Click Add
But the problem I have is that the drop down list doesn't show MovieDB (McvDemo.Models) in Model Class and Data Context Class to be selected. Can anyone please help me? Thanks.
You should just be able to recompile (Shift-Ctrl-B) and then try it again - it will be there. Otherwise you can always just declare it yourself at the top of a blank view, but that will not provide the scaffolding that the generator does:
#model MvcDemo.Models.MovieDB;
I recompiled but that did not fix the issue for me and yes I am doing the same thing and ran into the same exact issue. The problem for me was caused by visual web developer not being able to connect to my Movies database. I had to change the definition of my connectionString within web.config like this:
<add name="MovieDBContext"connectionString="Data Source=c:\sites\w3schools_demo\MvcDemo2\MvcDemo2\App_Data\Movies.sdf" providerName="System.Data.SqlServerCe.4.0"/>
If you are having this issue you will need to change the "Data Source" path to point to your Movies.sdf database file.

MVC 2 Data annotations problem

Going mad now. I have a MVC solution that i've upgraded from MVC 1 to 2. It all works fine.... except the Validation!
Here's some code:
In the controller:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using MF.Services.Authentication;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace MF.Controllers
{
//basic viewmodel
public class LogOnViewData
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
}
[HandleError]
public class AccountController : Controller
{
[HttpPost]
public ActionResult LogOn(LogOnViewData lvd, string returnUrl)
{
if (ModelState.IsValid)
{
//do stuff - IsValid is always true
}
}
}
}
The ModelState is always valid. The model is being populated correctly however. Therefore, if I leave both username and password blank, and post the form the model state is still valid. Argh!
Extra info: using structure map for IoD. Previously, before upgrading to MVC 2 was using the MS data annotation library so had this in my global.asax.cs:
ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
Have removed that now.
I'm sure i'm doing something really basic and wrong. If someone could point it out that would be marvellous.
Cheers
Half way through the development of MVC2, they went from input validation to model validation, which should in all cases validate your object completely. Make sure you're using the latest version (RTM).
However, [Required] merely indicates the attribute must not be null. Unfortunately, String.Empty -which is the default for strings- is not null, so model validation will pass for empty strings.
See this post by Brad Wilson for important details.
As a solution, you could use the [RegularExpression("....")] to impose restrictions on the minimum string length and allowed characters.

Resources