Person.cs
[Table("People")]
public class Person
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
}
DataContext.cs
public class DataContext : DbContext
{
public DbSet<Person> People { get; set; }
public DataContext()
: base("name=DataConnection")
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
Calc.cs
public class Calc
{
public int Sum(int num1, int num2)
{
return num1 + num2;
}
}
Using Nuget installed the following package:
Install-Package EFCodeFirst.SqlServerCompact
I added the dll reference to previously created
I modified the following actions on HomeController
HomeController.cs
public ActionResult Index()
{
ViewBag.Message = "Sample Security Error";
return View();
}
public ActionResult Sum(int num1, int num2)
{
ViewBag.Message = "Sample Security Error";
return View("Index", num1 + num2);
}
Added a new controller
PeopleController.cs
public ActionResult Index()
{
using (var db = new DataContext())
{
var people = from p in db.People
select p;
return View(people.ToList());
}
}
I changed the View/Home
with the following code:
Index.cshtml
@model int?
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
@Html.ActionLink("Sum", "Sum", new { num1 = 2, num2 = 4})
@if (Model.HasValue)
{
<p>The value is: @Model.Value</p>
}
else
{
<p>No value</p>
}
New View/People
Index.cshtml
@model IEnumerable<Error.SecurityException.Model.Person>
@{
ViewBag.Title = "People";
}
<h2>@ViewBag.Title</h2>
<ul>
@foreach (var item in Model)
{
<li>@item.Name</li>
}
</ul>
In Web.config I added the connection string
<connectionStrings>
<add name="DataConnection" connectionString="Data Source=|DataDirectory|Data.sdf;" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
App_Data
folder created the database Data.sdf
with the same structure as the Person.cs
class.
Using Nuget installed the following package:
Install-Package EFCodeFirst.SqlServerCompact
Result
The code in local IIS worked perfectly!
Sum values
Display persons
Error
When I published the site, while trying to display the persons, a security error message is displayed;
System.Security.SecurityException: Request failed.
The sum values is normally done!
I published the project this address.
Clicking the People
menu you can see the error!
Compact the sample project and published at this address.
I appreciate the help!
This error was fixed in version 4.1 of the Entity Framework! It was a bug of their entity framework CF