我使用DB第一种方法
我有3个表,其中包含“订单”列(客户订单记录)。我想创建类似的逻辑来重新排序我的.NET应用程序,所以,我想使用基类。我创建了以下结构:
但是项目无法编译,没有为每个3个表映射列Order,我看不到映射它的方法:
怎么说,Order列应该映射到父类的Order属性?
更新:
否则我必须创建类似的方法:
public interface IOrder
{
int Order { get; set; }
}
public partial class EscortDescription : IOrder
{
}
public partial class EscortGroup : IOrder
{
}
public partial class EscortItem : IOrder
{
}
private async Task ReorderEscortAsync(Infrastructure.IOrder item1, Infrastructure.IOrder item2)
{
Random rnd = new Random();
if (item1 == null)
throw new ArgumentNullException("firstItem");
if (item2 == null)
throw new ArgumentNullException("secondItem");
int tmpItem1Order = item1.Order;
int tmpItem2Order = item2.Order;
item1.Order = rnd.Next(int.MinValue, -1);
item2.Order = rnd.Next(int.MinValue, -1);
_db.SaveChanges();
item1.Order = tmpItem2Order;
item2.Order = tmpItem1Order;
await _db.SaveChangesAsync();
}
public async Task EscortGroupItemUpAsync(int ItemID)
{
var firstItem = (from i in _db.EscortItems where i.ID == ItemID select i).FirstOrDefault();
if (firstItem == null)
throw new Domain.RecordNotFoundException<int>(ItemID, "ID", "EscortItems");
var secondItem = (from i in _db.EscortItems where i.Order < firstItem.Order orderby i.Order descending select i).FirstOrDefault();
if (secondItem != null)
await ReorderEscortAsync(firstItem, secondItem);
else
throw new FirstRecordException();
}
public async Task EscortGroupItemDownAsync(int ItemID)
{
var secondItem = (from i in _db.EscortItems where i.ID == ItemID select i).FirstOrDefault();
if (secondItem == null)
throw new Domain.RecordNotFoundException<int>(ItemID, "ID", "EscortItems");
var firstItem = (from i in _db.EscortItems where i.Order > secondItem.Order orderby i.Order ascending select i).FirstOrDefault();
if (firstItem != null)
await ReorderEscortAsync(firstItem, secondItem);
else
throw new LastRecordException();
}
public async Task EscortGroupUpAsync(int ItemID)
{
var firstItem = (from i in _db.EscortGroups where i.ID == ItemID select i).FirstOrDefault();
if (firstItem == null)
throw new Domain.RecordNotFoundException<int>(ItemID, "ID", "EscortGroups");
var secondItem = (from i in _db.EscortGroups where i.Order < firstItem.Order orderby i.Order descending select i).FirstOrDefault();
if (secondItem != null)
await ReorderEscortAsync(firstItem, secondItem);
else
throw new FirstRecordException();
}
public async Task EscortGroupDownAsync(int ItemID)
{
var secondItem = (from i in _db.EscortGroups where i.ID == ItemID select i).FirstOrDefault();
if (secondItem == null)
throw new Domain.RecordNotFoundException<int>(ItemID, "ID", "EscortGroups");
var firstItem = (from i in _db.EscortGroups where i.Order > secondItem.Order orderby i.Order ascending select i).FirstOrDefault();
if (firstItem != null)
await ReorderEscortAsync(firstItem, secondItem);
else
throw new LastRecordException();
}
我想有一个方法并将基类的对象作为参数传递
答案 0 :(得分:1)
如果我理解你,OrderBase
没有表格。这意味着这是一个每个具体的表格(TPC)映射。 This link表明,在代码优先的情况下,映射TPC并不难。
数据库优先和TPC不能一起玩得很好。您必须编辑EDMX才能完成此操作。
我用你的两个课做了。目前,在您的EDMX的CS映射内容部分中,您将找到如下内容:
<EntitySetMapping Name="OrderBases">
<EntityTypeMapping TypeName="IsTypeOf(TPCModel.EcortGroup)">
<MappingFragment StoreEntitySet="EcortGroup">
<ScalarProperty Name="GroupName" ColumnName="GroupName" />
<ScalarProperty Name="ID" ColumnName="ID" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(TPCModel.EscortItem)">
<MappingFragment StoreEntitySet="EscortItem">
<ScalarProperty Name="Escort" ColumnName="Escort" />
<ScalarProperty Name="EcortGroup_ID" ColumnName="EcortGroup_ID" />
<ScalarProperty Name="ID" ColumnName="ID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
这告诉哪些列映射到哪些属性。如您所见,Order
丢失了。但你可以手动添加它们,EF会很高兴,EDMX仍然可以在设计师中打开:
<EntityTypeMapping TypeName="IsTypeOf(TPCModel.EcortGroup)">
<MappingFragment StoreEntitySet="EcortGroup">
<ScalarProperty Name="GroupName" ColumnName="GroupName" />
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="Order" ColumnName="Order" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(TPCModel.EscortItem)">
<MappingFragment StoreEntitySet="EscortItem">
<ScalarProperty Name="Escort" ColumnName="Escort" />
<ScalarProperty Name="EcortGroup_ID" ColumnName="EcortGroup_ID" />
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="Order" ColumnName="Order" />
</MappingFragment>
</EntityTypeMapping>
HOWEVER :从数据库更新模型时,修改将会消失。每次执行此操作时,您都必须再次添加它们。如果可以,我强烈建议您将代码移植到代码优先。