自定义值类型,EF代码优先和路由

时间:2015-11-19 23:58:16

标签: c# asp.net-mvc entity-framework asp.net-web-api routing

在我们的WebApi项目中,我们使用EF CodeFirst方法。我们还使用两种类型的数据库:SQL Server和MySQL。所有表都有字段Sub Text2_AfterUpdate() Dim VarA As Variant Dim VarW As Variant VarW = Left([Forms]![Return Student Info]![Text2], InStr([Forms]![Return Student Info]![Text2], "-") - 1) VarA = DLookup("[Name First]&' '&[Name Last]&' '&[Student Number]", "Student Bookings (term 84)", _ "[Room Space Description] like 'VarW'?? ") MsgBox (VarW) Text0.Value = VarA End Sub ,但在SQL Server数据库中,此字段具有ID数据类型,在MySQL数据库中,此字段为int并包含char(36)

为了解决这个问题,我创建了一个自定义值类型,如GUID,并更改了所有模型类,以使用该类型IdType

int

然后我配置了DbContext(例如SQL Server)

public class Document
{
  public IdType ID { get; set; }
  public string DocumentNm { get; set; }
  ...
}

...并更改了存储库:

modelBuilder.Properties<IdType>().Configure(c => c.HasColumnType("int"));

之后,当我试图去public interface IRepository<T> where T : IEntity { IQueryable<T> GetAll(); T GetById(IdType id); ... } ,它给了我一个错误:

  

找到了与请求匹配的多个操作:\ r \ n获取类型   类型上的WebUI.Controllers.API.DocumentController \ r \ n \ nGetById   WebUI.Controllers.API.DocumentController

我使用路由的默认设置。以下是DocumentController中的http://localhost:7081/api/Document方法:

[HttpGet]

我该如何解决这个问题?这可能是public HttpResponseMessage Get() { ... } public HttpResponseMessage GetById(IdType id) { ... } 错误实施的原因吗?

P.S。我按照here所述为int值创建了IdType。如果我需要添加更多信息,请告诉我。

更新

DocumentController:

IdType

我的存储库:

public HttpResponseMessage GetById(IdType id)
{
    var entity = repository.GetById(id);

    if (entity == null)
    {                
       return ErrorMsg(HttpStatusCode.NotFound, string.Format("No {0} with ID = {1}", GenericTypeName, id););
    }

    return Request.CreateResponse(HttpStatusCode.OK, entity);
 }

1 个答案:

答案 0 :(得分:0)

似乎尚未在当前版本的Entity Framework中实现

task on GitHub

中所述
  

我们目前正计划在完成此功能后点亮此功能   EF7的初始RTM。