1
  1. 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

  1. 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

Sum value

Display persons

Dados do Data.sdf

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!

bkaid
  • 51,465
  • 22
  • 112
  • 128
ridermansb
  • 10,779
  • 24
  • 115
  • 226
  • Do you have write permissions on the sdf file? – Buildstarted Jul 14 '11 at 23:49
  • Nobody is going to download a 11MB RAR file to debug your issue. Part of the problem might be that you are using an old CTP and not the release package, EntityFramework.SqlServerCompact – Diego Mijelshon Jul 15 '11 at 00:50
  • Version 0.8 (updated via Nuget) see image: http://i1118.photobucket.com/albums/k610/ridermansb/Nuget.png I removed the packages and dlls of the project. Follow the link for download: http://teste.sextaigreja.com.br/Error.SecurityExeption-min.rar I have both the permission and the file folder Data.sdf – ridermansb Jul 16 '11 at 14:05

3 Answers3

0

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.

Mj. Logan
  • 35
  • 2
  • 6
0

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

Matt Griffiths
  • 1,142
  • 8
  • 26
  • Locally it works perfectly! Changed to `` and error remained. The funny thing is that the error happens only on this host. Host the site on another host and it worked perfectly. But I had to add the line of code `` – ridermansb Jul 18 '11 at 15:02
0

This error was fixed in version 4.1 of the Entity Framework! It was a bug of their entity framework CF

ridermansb
  • 10,779
  • 24
  • 115
  • 226