SecurityException: Request failed. - Entity Framework Code First (Sql Compact 4)
- I created an assembly (dll) with the following classes:
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 created a new site MVC 3
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!
Since I can't open your solution because it's configured to run in IIS... Can you add;
<trust level="Medium"/>
In your local projects web.config (under system.web) and run it locally? Do you get the same error? If you do, its a partial trust issue!
Matt
This error was fixed in version 4.1 of the Entity Framework! It was a bug of their entity framework CF
I got here looking for a Security Exception that was being fired in my own ASP.Net MVC application. Although my problem might not be exactly the same (it was raising a System.Configuration.ConfigurationPermission when running any page that called DbContext) I found a straightforward solution that could be suitable for many cases. I changed my web.config, adding a requirePermission="false" in the custom section, as follows:
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Hope this helps somebody with the same problem.
精彩评论