我试图设置一个相当复杂的数据模型,并且尽管sql方面非常直接,但是我在我的数据库中的流畅配置中遇到了一个奇怪的问题上下文。
我所拥有的是3个表,其中2个表共享一个名为tasks的公用表,如下所示:
[PK][Id]
[UserId]
[TaskType]
[CreatedDate]
[CompletedDate]
其他三个表格如下:
打印:
[PK][Id]
[TaskId]
[TaskItemId]
[Scnumber]
[Address]
[City]
[Province]
[PostalCode]
[Country]
取消:
[PK][Id]
[TaskId]
[TaskItemId]
[Scnumber]
如果我在SQL中做任何事情都不会有任何问题,但在EntityFramework中,当我尝试添加一个与之关联的打印项目的任务时,我的数据库上下文出现了问题。
public partial class Tasks
{
public Tasks()
{
Items = new HashSet<ITaskItem>();
}
public long Id { get; set; }
public string UserId { get; set; }
public byte TaskType { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? CompletedDate { get; set; }
public ICollection<ITaskItem> Items { get; set; }
}
public interface ITaskItem { }
public partial class Cancelations : ITaskItem
{
public long Id { get; set; }
public long TaskId { get; set; }
public string TaskItemId { get; set; }
public long Scnumber { get; set; }
public Tasks Task { get; set; }
}
public partial class Print : ITaskItem
{
public long Id { get; set; }
public long TaskId { get; set; }
public string TaskItemId { get; set; }
public long Scnumber { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public Tasks Task { get; set; }
}
所以在我的数据库上下文中,当我构建取消和打印时,当我指定关系时我明确地转换为正确的类型,但是我似乎没有工作和而是转换为错误的类型。在我尝试使用打印项创建任务的情况下,我收到了一个转换错误,但是当我添加取消项时,没有问题。我无法弄清楚为什么会这样。
public partial class DemoDbContext : DbContext
{
public virtual DbSet<Cancelations> Cancelations { get; set; }
public virtual DbSet<Print> Print { get; set; }
public virtual DbSet<Tasks> Tasks { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"connection string here");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Print>(entity =>
{
entity.ToTable("Print", "cdi");
entity.HasIndex(e => e.TaskItemId)
.HasName("UQ_Print_TaskItemId")
.IsUnique();
entity.Property(e => e.Address).HasMaxLength(150);
entity.Property(e => e.City).HasMaxLength(75);
entity.Property(e => e.Country).HasColumnType("char(2)");
entity.Property(e => e.PostalCode).HasMaxLength(15);
entity.Property(e => e.ProcessedDate)
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.Province).HasMaxLength(5);
entity.Property(e => e.TaskItemId)
.IsRequired()
.HasColumnType("nchar(39)")
.HasDefaultValueSql("(concat('P-',newid()))");
entity.HasOne(d => d.Task)
.WithMany(p => ((ICollection<Print>)p.Items))
.HasForeignKey(d => d.TaskId)
.HasConstraintName("FK_Print_Tasks");
});
modelBuilder.Entity<Cancelations>(entity =>
{
entity.ToTable("Cancelations", "cdi");
entity.HasIndex(e => e.TaskItemId)
.HasName("UQ_Cancelations_TaskItemId")
.IsUnique();
entity.Property(e => e.ProcessedDate)
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.TaskItemId)
.IsRequired()
.HasColumnType("nchar(39)")
.HasDefaultValueSql("(concat('C-',newid()))");
entity.HasOne(d => d.Task)
.WithMany(p => ((ICollection<Cancelations>)p.Items))
.HasForeignKey(d => d.TaskId)
.HasConstraintName("FK_Cancelations_Tasks");
});
modelBuilder.Entity<Tasks>(entity =>
{
entity.ToTable("Tasks", "cdi");
entity.Property(e => e.CompletedDate).HasColumnType("datetime");
entity.Property(e => e.CreatedDate).HasColumnType("datetime");
entity.Property(e => e.UserId)
.IsRequired()
.HasMaxLength(36);
});
}
}
当我尝试执行以下操作时出现此错误:
var context = new DemoDbContext();
var task = new Tasks
{
CreatedDate = DateTime.Now
};
var items = new List<ITaskItem>
{
new Print
{
Scnumber = 0000000001,
Address = "123 Demo St",
City = "Toronto",
Province = "ON",
PostalCode = "H0H0H0",
Country = "CA",
ProcessedDate = DateTime.Now
},
new Print
{
Scnumber = 0000000001,
Address = "123 Demo St",
City = "Toronto",
Province = "ON",
PostalCode = "H0H0H0",
Country = "CA",
ProcessedDate = DateTime.Now
},
};
task.Items = items; //cancellations;
task.UserId = "moo cow";
task.TaskType = 1;
try
{
context.Tasks.Add(task);
context.SaveChanges();
} catch(Exception ex)
{
Console.WriteLine(ex);
}
Unable to cast object of type 'temp.Models.Print' to type 'temp.Models.Cancelations'.