我想在一個SQLite資料庫中使用Guid作為主鍵。 到目前為止,一切都運行得很好,只是Guid值被保存在表的大寫字母列中。這種行為可以改變嗎? 我需要它的小寫字母。
Update
<我們需要小寫的 Uuid,因為我們用 Dapper 撰寫的應用程式中的所有 Uuid 都以小寫保存。
由于我們現在想要切換到 EF Core,如果未來所有的 Uuid 都是大寫字母就不好了,因為這將不再是統一的。
我使用EF Core和代碼優先的方法。
下面是我使用的一些代碼:
聯系人模型
聯系人模型 DbContext重寫: 物體型別配置: 插入遷移的部分: 結果: uj5u.com熱心網友回復: 在我的例子中,解決方案是 新EntityTypeConfiguration 感謝所有花時間幫助我的人。
標籤: 上一篇:只從SQLITE中洗掉第一個結果
public class Contact : ObservableObject
{
#region Properties。
public virtual ICollection< Address> Addresses { get; set; }
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
#endregion。
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var stringBuilder = new SqliteConnectionStringBuilder
{
BrowsableConnectionString = true,
DataSource = $"{Path.GetDirectoryName(typeof(App).Assembly.Location)}database.db3"/span>。
模式 = SqliteOpenMode.ReadWriteCreate。
};
optionsBuilder.UseSqlite(stringBuilder.ConnectionString);
Console.WriteLine(stringBuilder.ConnectionString)。
base.OnConfiguring(optionsBuilder)。
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new AddressConfig())。
builder.ApplyConfiguration(new ContactConfig())。
public class ContactConfig : IEntityTypeConfiguration<Contact>
{
public void Configure(EntityTypeBuilder< Contact> builder)
{
builder.HasKey(contact => contact.Id) 。
builder.Property(span class="hljs-params">contact => contact. Id).IsRequired().ValueGeneratedOnAdd()。
builder.HasMany(contact => contact.Addresses) 。
builder.HasData(new Contact)
{
Id = Guid.NewGuid()。
Addresses = new List<Address>()。
FirstName = "Max"/span>,
LastName = "Mayer"migrationBuilder.InsertData(
table: "Contact"。
列。new[] { "Id", "FirstName", "LastName" },
值。new object[] { new Guid("ee4ecac-6250-4f29-a883-15cf32b28520"), "Max", "Mayer" })。)
HasConversion(new GuidToStringConverter())。
在我做了以下修改后,UUID被保存為小寫字母。
public class ContactConfig : IEntityTypeConfiguration<Contact>
{
public void Configure(EntityTypeBuilder< Contact> builder)
{
builder.HasKey(contact => contact.Id) 。
builder.Property(span class="hljs-params">contact => contact.Id)。 HasConversion(new GuidToStringConverter() )。
builder.HasMany(contact => contact.Addresses) 。
builder.HasData(new Contact)
{
Id = Guid.NewGuid()。
Addresses = new List<Address>()。
FirstName = "Max"/span>,
LastName = "Mayer"

