自定义值类型、EF Code First 和路由
Custom value type, EF Code First and routing
在我们的 WebApi 项目中,我们使用 EF CodeFirst 方法。我们还使用 2 种类型的数据库:SQL 服务器和 MySQL。所有表都有字段 ID
,但在 SQL 服务器数据库中,该字段具有 int
数据类型,在 MySQL 数据库中,该字段为 char(36)
并包含 GUID
。
为了解决这个问题,我创建了一个自定义值类型,如 IdType
,并将所有模型 类 更改为使用该类型 insted int
:
public class Document
{
public IdType ID { get; set; }
public string DocumentNm { get; set; }
...
}
然后我配置了 DbContext(例如 SQL 服务器)
modelBuilder.Properties<IdType>().Configure(c => c.HasColumnType("int"));
...并更改了存储库:
public interface IRepository<T> where T : IEntity
{
IQueryable<T> GetAll();
T GetById(IdType id);
...
}
在那之后,当我尝试去例如http://localhost:7081/api/Document
,它给我一个错误:
Multiple actions were found that match the request: \r\nGet on type
WebUI.Controllers.API.DocumentController\r\nGetById on type
WebUI.Controllers.API.DocumentController
我使用路由的默认设置。这是来自 DocumentController 的 [HttpGet]
方法:
public HttpResponseMessage Get() { ... }
public HttpResponseMessage GetById(IdType id) { ... }
我该如何解决这个问题?这可能是 IdType
执行不正确的原因吗?
P.S。我按照 here 所述为 int 值创建了 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);
}
我的存储库:
public virtual T GetById(IdType id)
{
return GetAll().FirstOrDefault(x => x.ID == id);
}
public virtual IQueryable<T> GetAll()
{
return entities = context.Set<T>();
}
Entity Framework
当前版本好像还没有实现
中所述
we're currently planning to work on lighting this feature up after our
initial RTM of EF7.
在我们的 WebApi 项目中,我们使用 EF CodeFirst 方法。我们还使用 2 种类型的数据库:SQL 服务器和 MySQL。所有表都有字段 ID
,但在 SQL 服务器数据库中,该字段具有 int
数据类型,在 MySQL 数据库中,该字段为 char(36)
并包含 GUID
。
为了解决这个问题,我创建了一个自定义值类型,如 IdType
,并将所有模型 类 更改为使用该类型 insted int
:
public class Document
{
public IdType ID { get; set; }
public string DocumentNm { get; set; }
...
}
然后我配置了 DbContext(例如 SQL 服务器)
modelBuilder.Properties<IdType>().Configure(c => c.HasColumnType("int"));
...并更改了存储库:
public interface IRepository<T> where T : IEntity
{
IQueryable<T> GetAll();
T GetById(IdType id);
...
}
在那之后,当我尝试去例如http://localhost:7081/api/Document
,它给我一个错误:
Multiple actions were found that match the request: \r\nGet on type WebUI.Controllers.API.DocumentController\r\nGetById on type WebUI.Controllers.API.DocumentController
我使用路由的默认设置。这是来自 DocumentController 的 [HttpGet]
方法:
public HttpResponseMessage Get() { ... }
public HttpResponseMessage GetById(IdType id) { ... }
我该如何解决这个问题?这可能是 IdType
执行不正确的原因吗?
P.S。我按照 here 所述为 int 值创建了 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);
}
我的存储库:
public virtual T GetById(IdType id)
{
return GetAll().FirstOrDefault(x => x.ID == id);
}
public virtual IQueryable<T> GetAll()
{
return entities = context.Set<T>();
}
Entity Framework
当前版本好像还没有实现 中所述we're currently planning to work on lighting this feature up after our initial RTM of EF7.