简体   繁体   English

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

[英]Custom value type, EF Code First and routing

In our WebApi project we use EF CodeFirst approach. 在我们的WebApi项目中,我们使用EF CodeFirst方法。 Also we use 2 types of databases: SQL Server and MySQL. 另外,我们使用两种类型的数据库:SQL Server和MySQL。 All tables have the field ID , but in SQL Server database this field has int data type, in MySQL database this field is char(36) and contains GUID . 所有表都有字段ID ,但是在SQL Server数据库中,此字段具有int数据类型,在MySQL数据库中,此字段为char(36)并包含GUID

To solve the problem I created a custom value type like IdType and changed all model classes to use that type insted int : 为了解决这个问题,我创建了一个像IdType这样的自定义值类型,并更改了所有模型类以使用该类型insted int

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

Then I configured the DbContext (eg for SQL Server) 然后,我配置了DbContext(例如,对于SQL Server)

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

...and changed repository: ...并更改存储库:

public interface IRepository<T> where T : IEntity
{
  IQueryable<T> GetAll();
  T GetById(IdType id);
  ...
}

After that, when I try to go to eg http://localhost:7081/api/Document , it gives me an error: 之后,当我尝试转到例如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 找到多个与请求匹配的操作:\\ r \\ nWebUI.Controllers.API.DocumentController类型的\\ r \\ nGetById WebUI.Controllers.API.DocumentController类型的

I use default settings of routing. 我使用路由的默认设置。 Here is [HttpGet] methods from DocumentController: 这是DocumentController的[HttpGet]方法:

public HttpResponseMessage Get() { ... }
public HttpResponseMessage GetById(IdType id) { ... }

How can I solve the problem? 我该如何解决这个问题? Could this be the cause of incorrect implementation of IdType ? 这可能是IdType实施不正确的IdType吗?

PS I created IdType for int values as described here . PS我按此处所述为int值创建了IdType。 if I have to add more informations, please let me know. 如果需要添加更多信息,请告诉我。

UPDATE UPDATE

DocumentController: DocumentController:

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);
 }

My repository: 我的资料库:

 public virtual T GetById(IdType id)
 {
    return GetAll().FirstOrDefault(x => x.ID == id);
 }
 public virtual IQueryable<T> GetAll()
 {
   return entities = context.Set<T>();
 }

It seems that it not implemented yet in current version of Entity Framework 似乎在当前版本的Entity Framework中尚未实现

And as mentioned in task on GitHub 正如GitHub上的任务中所述

we're currently planning to work on lighting this feature up after our initial RTM of EF7. 我们目前正计划在我们最初的EF7 RTM之后启用此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM