如何为 TPH class 设置 table 名称?
How to set the table name for TPH class?
我知道可以在 DbContext 的 OnModelCreating 覆盖中设置 table 名称。
但是,当业务对象继承自基础 class.
时,我 运行 遇到了尝试执行此操作的问题
我的业务对象(来自 xaf)是
[DefaultProperty(nameof(UserName))]
public class ApplicationUser : PermissionPolicyUser,
IObjectSpaceLink, ISecurityUserWithLoginInfo {
public ApplicationUser() : base() {
UserLogins = new List<ApplicationUserLoginInfo>();
}
[Browsable(false)]
[DevExpress.ExpressApp.DC.Aggregated]
public virtual IList<ApplicationUserLoginInfo> UserLogins { get; set; }
IEnumerable<ISecurityUserLoginInfo> IOAuthSecurityUser.UserLogins => UserLogins.OfType<ISecurityUserLoginInfo>();
ISecurityUserLoginInfo ISecurityUserWithLoginInfo.CreateUserLoginInfo(string loginProviderName, string providerUserKey) {
ApplicationUserLoginInfo result = ((IObjectSpaceLink)this).ObjectSpace.CreateObject<ApplicationUserLoginInfo>();
result.LoginProviderName = loginProviderName;
result.ProviderUserKey = providerUserKey;
result.User = this;
return result;
}
}
我的DbContext是这样的
public class JTEFCoreDbContext : DbContext {
public JTEFCoreDbContext(DbContextOptions<JTEFCoreDbContext> options) : base(options)
{
}
public DbSet<ApplicationUser> Users { get; set; }
// etc
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<ApplicationUserLoginInfo>(b =>
{
modelBuilder.Entity<ApplicationUser>().ToTable("PermissionPolicyUsers");
// etc
我的单元测试是这样的
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var connectionString =
"Server=myserver;Database=mydb;Integrated Security=false;MultipleActiveResultSets=True;user=sa;pwd=mypassword";
var optionsBuilder = new DbContextOptionsBuilder<JTEFCoreDbContext>().UseSqlServer(connectionString);
var options = optionsBuilder.Options;
var db = new JTEFCoreDbContext(options);
var user =db.Users.FirstOrDefault(); // fails with error
}
}
}
错误是
Microsoft.Data.SqlClient.SqlException: Invalid object name 'PermissionPolicyUser'
测试项目为
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JT2Core.Module\JT2Core.Module.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
模块项目是
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Deterministic>false</Deterministic>
<AssemblyVersion>1.0.*</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Configurations>Debug;Release;EasyTest</Configurations>
</PropertyGroup>
<ItemGroup>
<None Remove="Model.DesignedDiffs.xafml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Model.DesignedDiffs.xafml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DevExpress.ExpressApp" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.CodeAnalysis" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.ConditionalAppearance" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.EFCore" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.Validation" Version="21.2.5" />
<PackageReference Include="DevExpress.Persistent.Base" Version="21.2.5" />
<PackageReference Include="DevExpress.Persistent.BaseImpl.EFCore" Version="21.2.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
</ItemGroup>
</Project>
我设法通过将 类 设置为使用 table 名称
来进行单元测试
在 OnModelCreating
modelBuilder.Entity<ApplicationUser>().ToTable("PermissionPolicyUsers");
modelBuilder.Entity<PermissionPolicyUser>().ToTable("PermissionPolicyUsers");
交叉链接到 Dev Express Ticket
我知道可以在 DbContext 的 OnModelCreating 覆盖中设置 table 名称。 但是,当业务对象继承自基础 class.
时,我 运行 遇到了尝试执行此操作的问题我的业务对象(来自 xaf)是
[DefaultProperty(nameof(UserName))]
public class ApplicationUser : PermissionPolicyUser,
IObjectSpaceLink, ISecurityUserWithLoginInfo {
public ApplicationUser() : base() {
UserLogins = new List<ApplicationUserLoginInfo>();
}
[Browsable(false)]
[DevExpress.ExpressApp.DC.Aggregated]
public virtual IList<ApplicationUserLoginInfo> UserLogins { get; set; }
IEnumerable<ISecurityUserLoginInfo> IOAuthSecurityUser.UserLogins => UserLogins.OfType<ISecurityUserLoginInfo>();
ISecurityUserLoginInfo ISecurityUserWithLoginInfo.CreateUserLoginInfo(string loginProviderName, string providerUserKey) {
ApplicationUserLoginInfo result = ((IObjectSpaceLink)this).ObjectSpace.CreateObject<ApplicationUserLoginInfo>();
result.LoginProviderName = loginProviderName;
result.ProviderUserKey = providerUserKey;
result.User = this;
return result;
}
}
我的DbContext是这样的
public class JTEFCoreDbContext : DbContext {
public JTEFCoreDbContext(DbContextOptions<JTEFCoreDbContext> options) : base(options)
{
}
public DbSet<ApplicationUser> Users { get; set; }
// etc
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<ApplicationUserLoginInfo>(b =>
{
modelBuilder.Entity<ApplicationUser>().ToTable("PermissionPolicyUsers");
// etc
我的单元测试是这样的
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var connectionString =
"Server=myserver;Database=mydb;Integrated Security=false;MultipleActiveResultSets=True;user=sa;pwd=mypassword";
var optionsBuilder = new DbContextOptionsBuilder<JTEFCoreDbContext>().UseSqlServer(connectionString);
var options = optionsBuilder.Options;
var db = new JTEFCoreDbContext(options);
var user =db.Users.FirstOrDefault(); // fails with error
}
}
}
错误是
Microsoft.Data.SqlClient.SqlException: Invalid object name 'PermissionPolicyUser'
测试项目为
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JT2Core.Module\JT2Core.Module.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
模块项目是
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Deterministic>false</Deterministic>
<AssemblyVersion>1.0.*</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Configurations>Debug;Release;EasyTest</Configurations>
</PropertyGroup>
<ItemGroup>
<None Remove="Model.DesignedDiffs.xafml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Model.DesignedDiffs.xafml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DevExpress.ExpressApp" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.CodeAnalysis" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.ConditionalAppearance" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.EFCore" Version="21.2.5" />
<PackageReference Include="DevExpress.ExpressApp.Validation" Version="21.2.5" />
<PackageReference Include="DevExpress.Persistent.Base" Version="21.2.5" />
<PackageReference Include="DevExpress.Persistent.BaseImpl.EFCore" Version="21.2.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
</ItemGroup>
</Project>
我设法通过将 类 设置为使用 table 名称
来进行单元测试在 OnModelCreating
modelBuilder.Entity<ApplicationUser>().ToTable("PermissionPolicyUsers");
modelBuilder.Entity<PermissionPolicyUser>().ToTable("PermissionPolicyUsers");
交叉链接到 Dev Express Ticket