簡體   English   中英

如何將MongoDB模式遷移到SQL Server模型“ EF-Core”?

[英]How to migrate MongoDB schema to SQL Server model “EF-Core”?

我有以下MongoDB模式

const userSchema = new mongoose.Schema(
    {
        email: { type: String, unique: true },

        tokens: [{ accessToken: String, kind: String }],

        permissions: [Number],
        registration: { type: String, match: /(CLIENT|CUSTOMER|UNKOWN)/ },

        profile: {
            name: String,
            gender: String,
            location: String,
            website: String,
            picture: String,
        },

        clientCategories: [String],
    },
    { timestamps: true },
);

並且我想為SQL Server和EF Core實現相同的模型,我嘗試了以下實現

public class UserModel : IBaseUserModel
{
    [Key]
    public long Id { get; set; }

    [Required, EmailAddress]
    public string Email { get; set; }

    [Required]
    public ICollection<AccessToken> AccessTokens { get; set; }

    [Required]
    public ICollection<int> Permissions { get; set; }

    [Required, RegularExpression(@"^CLIENT|CUSTOMER|UNKOWN$")]
    public string Registration { get; set; }

    [Required]
    public Profile Profile { get; set; }
}

/// <summary> 
///     Basic user properties
/// </summary>
public interface IBaseUserModel
{
    long Id { get; set; }

    string Email { get; set; }

    ICollection<AccessToken> AccessTokens { get; set; }

    ICollection<int> Permissions { get; set; }

    string Registration { get; set; }

    Profile Profile { get; set; }
}

/// <summary>
///     The definition of the AccessToken
/// </summary>
public struct AccessToken { public string key, provider; }

/// <summary>
///     The definition of the User's Profile
/// </summary>
public struct Profile { public string name, gender, location, avatar; }

但我有一個錯誤

無法映射屬性'UserModel.AccessTokens',因為它的類型為'AccessToken []',它不是受支持的原始類型或有效的實體類型。 顯式映射此屬性,或使用'[NotMapped]'屬性或'OnModelCreating'中的'EntityTypeBuilder.Ignore'忽略它。

我知道EF Core支持type conversions ,但是我不知道如何使用該東西,這是我第一次使用此框架,謝謝。

旁注 ,我使用abstract類的原因是我將從public class CustomerModel : UserModel, ICustomer { }擴展其他類,例如public class CustomerModel : UserModel, ICustomer { }

type conversions對我來說還不是很清楚,但是現在很有意義,以下代碼可以解決問題

protected override void OnModelCreating (ModelBuilder modelBuilder)
{
    var StringListToStringConverter = new ValueConverter<ICollection<string>, string>(_strings => string.Join(";", _strings), _string => _string.Split(new[] { ';' }));
    var IntListToStringConverter = new ValueConverter<ICollection<int>, string>(_ints => string.Join(";", _ints), _string => Array.ConvertAll(_string.Split(new[] { ';' }), int.Parse));
    var AccessTokenToStringConverter = new ValueConverter<ICollection<AccessToken>, string>(_token => JsonConvert.SerializeObject(_token), _string => JsonConvert.DeserializeObject<ICollection<AccessToken>>(_string));
    var ProfileToStringConverter = new ValueConverter<Profile, string>(_token => JsonConvert.SerializeObject(_token), _string => JsonConvert.DeserializeObject<Profile>(_string));

    modelBuilder
        .Entity<UserModel>()
        .Property(e => e.Permissions)
        .HasConversion(IntListToStringConverter);

    modelBuilder
        .Entity<UserModel>()
        .Property(e => e.AccessTokens)
        .HasConversion(AccessTokenToStringConverter);

    modelBuilder
        .Entity<UserModel>()
        .Property(e => e.Profile)
        .HasConversion(ProfileToStringConverter);

    modelBuilder
        .Entity<ClientModel>()
        .Property(e => e.Categories)
        .HasConversion(StringListToStringConverter);
}

我認為將屬性轉換為字符串並將它們存儲在同一表中要比將每個屬性保存到新表中更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM