Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using EntityFramework.Explained._Tools.Helpers;
using Microsoft.EntityFrameworkCore;
using QuickPulse.Explains;
using QuickPulse.Explains.Text;
// Model and appdbcontext generated by AI


[DocFile]
public class ValueObjectProperties
{
// Value Object: CustomerName
public class CustomerName
{
public string FullName { get; private set; }

private CustomerName() { }

Check warning on line 16 in EntityFramework.Explained/Schema/Value-Objects/ValueObjectsAsProperties.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FullName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 16 in EntityFramework.Explained/Schema/Value-Objects/ValueObjectsAsProperties.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FullName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public CustomerName(string fullname)
{
FullName = fullname;
}

// Equality methods omitted for brevity
}

// Value Object: Address
public class Customer
{
public int Id { get; set; }

public CustomerName Name { get; set; }

public Customer() { }

Check warning on line 32 in EntityFramework.Explained/Schema/Value-Objects/ValueObjectsAsProperties.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 32 in EntityFramework.Explained/Schema/Value-Objects/ValueObjectsAsProperties.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public Customer(int id, CustomerName name)
{
Id = id;
Name = name;
}
}

public class AppDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer("DoesNotMatter"); // Required by EF, never actually used
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(customer =>
{
customer.HasKey(o => o.Id);

customer.OwnsOne(o => o.Name, cn =>
{
cn.Property(c => c.FullName).HasColumnName("CustomerFullName").IsRequired();
});
});
}
}


[Fact]
[DocHeader("Value Objects")]
[DocContent("checking what SQL is generated for value objects in model")]
public void VOChecker()
{
using var context = new AppDbContext();
var sql = context.Database.GenerateCreateScript();
var reader = LinesReader.FromText(sql);
Assert.Equal("CREATE TABLE [Customers] (", reader.NextLine());
Assert.Equal(" [Id] int NOT NULL IDENTITY,", reader.NextLine());
Assert.Equal(" [CustomerFullName] nvarchar(max) NOT NULL,", reader.NextLine());
Assert.Equal(" CONSTRAINT [PK_Customers] PRIMARY KEY ([Id])", reader.NextLine());
}
}
Loading