Files

48 lines
2.1 KiB
C#

using MessageService.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace MessageService.Infrastructure.Configs
{
public class MessageConfig : IEntityTypeConfiguration<Message>
{
public void Configure(EntityTypeBuilder<Message> builder)
{
builder.ToTable("messages");
builder.HasKey(x => x.Id);
builder.HasIndex(x => new { x.StreamKey, x.SequenceId });
builder.HasIndex(x => new { x.StreamKey, x.MsgType, x.State, x.SequenceId });
builder.ComplexProperty(x => x.Content, c =>
{
// 1. Fallback 是简单字符串,直接映射
c.Property(p => p.Fallback)
.HasMaxLength(255)
.IsRequired();
// 2. Body 是 object (JSON 载荷)
// ComplexProperty 无法直接处理 object 类型,通常将其序列化为 JSON 字符串存储
c.Property(p => p.RawBody)
.HasColumnName("Content_Body");
// 3. Ext 是 Dictionary<string, string>
// 同样推荐使用 HasConversion 映射为 JSON 字符串,或者在某些库中使用 JSONB 映射
// 映射 RawExt 字符串
c.Property(p => p.RawExt)
.HasColumnName("Content_Ext");
// 4. Quote 是嵌套的值对象 (QuoteInfo)
// ComplexProperty 支持嵌套定义
c.ComplexProperty(p => p.Quote, q =>
{
q.IsRequired(true); // 引用信息是可选的
q.Property(qi => qi.MessageId).HasColumnName("Quote_MsgId");
q.Property(qi => qi.SenderId).HasColumnName("Quote_SenderId");
q.Property(qi => qi.SenderName).HasMaxLength(50).HasColumnName("Quote_SenderName");
q.Property(qi => qi.MessageType).HasColumnName("Quote_MsgType");
q.Property(qi => qi.Preview).HasMaxLength(100).HasColumnName("Quote_Preview");
});
});
}
}
}