简体   繁体   English

实体框架字符串为guid

[英]Entity framework string to guid

Hi my domain models use string as there Ids. 嗨,我的域模型使用字符串作为ID。

public class TeamModel
{
    public string TeamId { get; set; }
    public string TeamName { get; set; }
}

Where as my entities use a Guid as there Ids 我的实体在哪里使用Guid as there ID

     public class Team
     {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid TeamId { get; set; }
        public string TeamName { get; set; }
     }

Now im using automapper to map models to entities, just wondering if there is a data annotation that would allow me to set my TeamId as a string in the entity, but it then auto formats to a guid and generates the new guid before storing in the database 现在我正在使用自动映射器将模型映射到实体,只是想知道是否有数据注释可以让我将TeamId设置为实体中的字符串,但是随后它会自动格式化为guid并生成新的guid,然后再存储在数据库

EDIT 编辑

The reason for string as Id in the domain model is because this is a research project with sql and mongoDb 在域模型中将字符串作为ID的原因是因为这是一个使用sql和mongoDb的研究项目

my mongoDb entity looks like this 我的mongoDb实体看起来像这样

public class Team
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string TeamId { get; set; }
    public string TeamName
}

So my domain will either pass a domain model to sql or mongo. 因此,我的域将通过域模型传递给sql或mongo。 Mongo converts string to ObjectId fine, I just need EF to be able to do something similar so I can use generic models in domain Mongo可以将字符串很好地转换为ObjectId,我只需要EF就能执行类似的操作,因此我可以在域中使用通用模型

Not I am aware of. 我不知道。 Suggest (if you are really required to have it of string type in your model) to make additional properties that will convert to string\\guid in a set and get accessors . 建议(如果确实需要在模型中使用字符串类型的字符串)建议制作其他属性,这些属性将在集合中转换为string \\ guid 并获取访问器

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

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