相关疑难解决方法(0)

默认情况下,在Entity Framework 4中禁用延迟加载

似乎在EF4中默认启用了延迟加载.至少,在我的项目中,我可以看到它的价值

dataContext.ContextOptions.LazyLoadingEnabled
Run Code Online (Sandbox Code Playgroud)

默认情况下为true.我不想延迟加载,我不想写:

dataContext.ContextOptions.LazyLoadingEnabled = false;
Run Code Online (Sandbox Code Playgroud)

每次我得到一个新的背景.那么有没有办法在整个项目中默认关闭它?

entity-framework lazy-loading entity-framework-4

60
推荐指数
4
解决办法
8万
查看次数

ObjectContext实例已被释放,不能再用于需要连接的操作

我有这个观点:

@model MatchGaming.Models.ProfileQuery
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>    
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Results", "Profiles")) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ProfileQuery</legend>
        @Html.EditorFor(model=>model.SearchString)
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
Run Code Online (Sandbox Code Playgroud)

我有HttpPost的这个控制器:

[HttpPost]
public ActionResult Results(ProfileQuery profileQuery)
{
    Debug.Write(profileQuery.SearchString);
    using(var db = new MatchGamingEntities())
    {
        var SearchUserName = db.Users.SingleOrDefault(a=> a.UserName.Contains(profileQuery.SearchString));
        var Users = from m in db.Users
                    join m2 in db.MyProfiles on m.UserId equals m2.UserId
                    where m.UserName == SearchUserName.UserName
                    select new UserViewModel
                    {
                        UserName …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework exception-handling asp.net-mvc-3

40
推荐指数
4
解决办法
8万
查看次数

System.ObjectDisposedException:ObjectContext实例已被释放,不能再用于需要连接的操作

我使用EF 4来检索员工列表.

public ContentResult AutoCompleteResult(string searchText)
{
    List<Employee> list = Employee.GetAllCurrentEmployees();
    List<Employee> filteredEmployees = list
        .Where(x => x.GetName().ToLower().Contains(searchText.ToLower()))
        .ToList();

    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
    var jsonString = jsonSerializer.Serialize(filteredEmployees).ToString();
    return Content(jsonString);
}
Run Code Online (Sandbox Code Playgroud)

列表检索正常,但是当我序列化它时,我得到了这个异常;

System.ObjectDisposedException: The ObjectContext instance has been
 disposed and can no longer be used for
 operations that require a connection.
     Generated: Wed, 17 Nov 2010 16:06:56 GMT

 System.ObjectDisposedException: The ObjectContext instance has been
 disposed and can no longer be used for operations that require a connection. 
 at
 System.Data.Objects.ObjectContext.EnsureConnection()
 at …
Run Code Online (Sandbox Code Playgroud)

c# asp.net json

27
推荐指数
3
解决办法
5万
查看次数

实体框架:多对多关系中的重复记录

我有以下实体框架代码的第一个代码.创建表并插入数据.但是俱乐部表中有重复的记录.

我的业务是: -

  1. 使用俱乐部创建应用创建俱乐部

  2. 使用person app创建人员

如何避免重复输入?

在此输入图像描述

    static void Main(string[] args)
    {
        Database.SetInitializer<NerdDinners>(new MyInitializer());

        CreateClubs();
        InsertPersons();

    }

    public static void CreateClubs()
    {

        string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
        using (var db = new NerdDinners(connectionstring))
        {

            Club club1 = new Club();
            club1.ClubName = "club1";

            Club club2 = new Club();
            club2.ClubName = "club2";

            Club club3 = new Club();
            club3.ClubName = "club3";

            db.Clubs.Add(club1);
            db.Clubs.Add(club2);
            db.Clubs.Add(club3);

            int recordsAffected = db.SaveChanges();


        }
    }

    public static Club GetClubs(string clubName)
    {
        string connectionstring = "Data …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework ef-code-first

10
推荐指数
1
解决办法
1万
查看次数

绑定时已释放ObjectContext实例

这是我的代码:

public class JobsRepository:BaseNewsletterRepository
{
   public IEnumerable<Job> GetJobs()
   {
        var jobs = Newsletterctx.Jobs.Include("Info").OrderBy(o => o.JobID);             
        return jobs.AsEnumerable();
   }
}

public class BusinessNewsletter
{
   public static IEnumerable<Job> GetJobs()
   {      
        using (JobsRepository jobsRepository = new JobsRepository())
        {
            return  jobsRepository.GetJobs();
        }    
    }
}    

rptJobs.DataSource = BusinessNewsletter.GetJobs();
rptJobs.DataBind();
Run Code Online (Sandbox Code Playgroud)

当我尝试绑定时,我得到错误:

ObjectContext实例已被释放,不能再用于需要连接的操作.

是唯一的解决方案,在BusinessNewsletter类中转换和返回List吗?

public static IList<Job> GetJobs()
{
    IList<Job> job;
    using (JobsRepository jobsRepository = new JobsRepository())
    {
        job = jobsRepository.GetJobs().ToList();
    }
    return job;
}
Run Code Online (Sandbox Code Playgroud)

另一个相关问题:

如何在不指定特定类型的情况下检索数据并将数据绑定到转发器(List<type>IEnumerable<type>)
如果我返回如下特定对象,我需要它:

var specificJob = from job …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework exception-handling

4
推荐指数
1
解决办法
6488
查看次数