EF Core DDD 多对一关系,外键始终为空

问题描述

关系:许多产品可以属于同一类别。简化了领域模型以关注问题。

我使用的是 EF Core v5.0。

Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(),bitmap.getHeight(),true); Log.d("yhjt","dsjahfo " + bitmap); PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(),i + 1).create(); PdfDocument.Page page = pdfDocument.startPage(pageInfo); Canvas canvas = page.getCanvas(); Paint paint = new Paint(); paint.setColor(Color.TRANSPARENT); canvas.drawPaint(paint); bitmap.compress(Bitmap.CompressFormat.JPEG,stream); bitmap = bitmap.copy(Bitmap.Config.RGB_565,false); Log.d("yhjt","dsjahfo " + bitmap); canvas.drawBitmap(bitmap,0f,null); Log.d("yhjt","dsjahfo " + page); pdfDocument.finishPage(page); bitmap.recycle(); try { parcelFileDescriptor.close(); stream.flush(); stream.close(); } catch (IOException e) { e.printstacktrace(); } } file = new File(Environment.getExternalStorageDirectory(),"MY_PDF"); if (!file.exists()) { file.mkdir(); } String txt = editText.getText().toString(); Log.d("edit","how " + txt); file1 = new File(file,txt + ".pdf"); try { fileOutputStream = new FileOutputStream(file1); } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printstacktrace(); } try { pdfDocument.writeto(fileOutputStream); } catch (IOException ioException) { ioException.printstacktrace(); } pdfDocument.close(); 模型类:

Product

public class Product: Entity,IAggregateRoot { protected Product() { } public Product(string name,Category category): this() { Name = name; Category = category; } public string Name { get; } public virtual Category Category { get; } } 模型类:

Category

产品配置:

public class Category: Entity
{
    protected Category() { }

    public Category(string name): this()
    {
        Name = name;
    }

    public string Name { get; }
}

类别配置:

public class ProductConfiguration: IEntityTypeConfiguration<Product>
{
    public void Configure(EntityTypeBuilder<Product> builder)
    {
        builder.ToTable("Product");

        builder.HasKey(x => x.Id);

        builder.Property(x => x.Id)
               .HasValueGenerator<StringValueGenerator>()
               .ValueGeneratedOnAdd();

        builder.Property(x => x.Name).Isrequired().HasMaxLength(500);
        
        builder.HasOne<Category>().WithMany();
    }

    #endregion
}

因此,有了这个配置和域模型,当尝试插入一些种子数据时,public class CategoryConfiguration: IEntityTypeConfiguration<Category> { public void Configure(EntityTypeBuilder<Category> builder) { builder.ToTable("Category"); builder.HasKey(x => x.Id); builder.Property(x => x.Id) .HasValueGenerator<StringValueGenerator>() .ValueGeneratedOnAdd(); builder.Property(x => x.Name) .Isrequired() .HasMaxLength(255); } #endregion } 属性(由 EF Core 创建的影子属性,而不是域模型中的属性)始终为空。

CategoryId

我在调用 public class ProductDbContextSeeder { public async Task SeedAsync(ProductDbContext context) { var category = await context.Categories.FirstOrDefaultAsync(x => x.Name == "Test Category"); if (category is null) { category = new Category("Test Category"); await context.Categories.AddAsync(category); await context.SaveChangesAsync(); } var product = await context.Products.FirstOrDefaultAsync(x => x.Name == "Test Product"); if (product is null) { product = new Product("Test Product",category); context.Products.Attach(product); await context.SaveChangesAsync(); } } } 之前检查了条目的状态,实体似乎处于正确状态,如下所示:

保存类别时:

保存产品时:

  • 产品 -> 已添加
  • 类别 -> 未更改

我错过了什么?感谢任何提示,指针。

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)