简短的问题.这是我的控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Milos_MovieStore.DAL;
using Milos_MovieStore.Models;
using Milos_MovieStore.DTO;
using System.Data.Entity;
namespace Milos_MovieStore.Controllers.Api
{
public class CustomersController : ApiController
{
private DBContext_MovieStore _dbcontext;
public CustomersController()
{
_dbcontext = new DBContext_MovieStore();
}
protected override void Dispose(bool disposing)
{
_dbcontext.Dispose();
}
// GET /api/customers
[HttpGet]
public IHttpActionResult GetCustomers()
{
List<Customer> customers = _dbcontext.Customers.Include(c => c.MembershipType).ToList();
return Ok(CustomersToDTOList(customers));
}
// GET /api/customers/1
[HttpGet]
public IHttpActionResult GetCustomer(int id)
{
Customer customer = _dbcontext.Customers.Include(c => …Run Code Online (Sandbox Code Playgroud) 使用VS Community2017。我尝试使用错误消息创建初始迁移:
实体框架核心和实体框架6均已安装。实体框架核心工具正在运行。对实体框架6使用'EntityFramework \ Add-Migration'。在程序集'Test_Project'中找不到DbContext。确保使用正确的程序集,并且类型既不是抽象的也不是泛型的。
...我的dbcontext中的代码:
protected override void OnModelCreating(DbModelBuilder mb)
{
base.OnModelCreating(mb);
mb.Entity<Stuff>().ToTable("Stuff");
}
public DbSet<Stuff> Stuff{ get; set; }
Run Code Online (Sandbox Code Playgroud) c# model-view-controller entity-framework dbcontext ef-migrations
Lock 参数中 object 的目的是什么。下面是一个例子:
public class TestThreading
{
private System.Object lockThis = new System.Object();
public void Process()
{
lock (lockThis)
{
// Access thread-sensitive resources.
}
}
}
Run Code Online (Sandbox Code Playgroud)
...这个'lockThis'对象是否将任何东西传递到Lock体中。为什么我不能在参数中使用没有对象的 Lock 语句。
只是想了解为什么destructor在实例被处置后在 USING 的范围之外被调用。我知道不需要destructor何时IDisposable实施。这是我的例子:
using System;
namespace Destructor
{
class Program
{
static void Main(string[] args)
{
using (var a = new A())
{
Console.WriteLine("... inside USING ...");
}
Console.WriteLine("...END...");
}
}
//............................................................
class A : IDisposable
{
public A()
{
Console.WriteLine("...Constructor called...");
}
~A()
{
Console.WriteLine("...Destructor called...");
}
public void Dispose()
{
Console.WriteLine("...Dispose called...");
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
...构造函数调用...
... 里面使用 ...
...处置称为...
...结尾...
...析构函数调用...