I would like to use Guid's as the primary key in a SQLite database. Everything works quite well so far, only the guid values are saved in the table column with capital letters. Can the behavior be changed? I need it in lower case.
Update
We need the Uuid in lower case because all Uuid in our applications that we have written with Dapper have this Uuid saved in lower case.
Since we now want to switch to EF Core, it would not be nice if all future Uuid were in capital letters because that would no longer be uniform.
I use EF Core and the code first approach.
Here some code that i use:
The Contact Model
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
}
DbContext Overrides:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var stringBuilder = new SqliteConnectionStringBuilder
{
BrowsableConnectionString = true,
DataSource = $"{Path.GetDirectoryName(typeof(App).Assembly.Location)}\\database.db3",
Mode = 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());
}
Entity Type Configuration:
public class ContactConfig : IEntityTypeConfiguration<Contact>
{
public void Configure(EntityTypeBuilder<Contact> builder)
{
builder.HasKey(contact => contact.Id);
builder.Property(contact => contact.Id).IsRequired().ValueGeneratedOnAdd();
builder.HasMany(contact => contact.Addresses);
builder.HasData(new Contact
{
Id = Guid.NewGuid(),
Addresses = new List<Address>(),
FirstName = "Max",
LastName = "Mayer"
});
}
}
Insert part of migration:
migrationBuilder.InsertData(
table: "Contact",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { new Guid("eee4ecac-6250-4f29-a883-15cf32b28520"), "Max", "Mayer" });
The result:
CodePudding user response:
In my case the solution was HasConversion(new GuidToStringConverter())
.
After I made the following changes, the UUID's were saved in lower case.
New EntityTypeConfiguration
public class ContactConfig : IEntityTypeConfiguration<Contact>
{
public void Configure(EntityTypeBuilder<Contact> builder)
{
builder.HasKey(contact => contact.Id);
builder.Property(contact => contact.Id).HasConversion(new GuidToStringConverter());
builder.HasMany(contact => contact.Addresses);
builder.HasData(new Contact
{
Id = Guid.NewGuid(),
Addresses = new List<Address>(),
FirstName = "Max",
LastName = "Mayer"
});
}
}
Thanks to everyone who took the time to help me.