[英]Insert into table without PK in Entity Framework Core
I'm currently using .NET Core 3.1, I want to make Integration some of my data into an existing application.我目前正在使用 .NET Core 3.1,我想将我的一些数据集成到现有应用程序中。 This application is using SQL Server as the database at first I want to use SSIS to integrate my data into this application but I just know that they are using SQL Server Express so I can't use SSIS. This application is using SQL Server as the database at first I want to use SSIS to integrate my data into this application but I just know that they are using SQL Server Express so I can't use SSIS.
So I decided to create a hangfire job to get my data (multiple sources) and insert it into the existing application using EF Core.因此,我决定创建一个 hangfire 作业来获取我的数据(多个来源)并使用 EF Core 将其插入到现有应用程序中。 But now I just know that the table doesn't have PK since It already running for quite some time and is not my application I can't just add the pk to the table.但是现在我只知道该表没有 PK,因为它已经运行了很长一段时间并且不是我的应用程序,我不能只将 pk 添加到表中。 When I try to Insert it using EF Core I got this error当我尝试使用 EF Core 插入它时出现此错误
System.InvalidOperationException: 'Unable to track an instance of type 'T1Item' because it does not have a primary key. System.InvalidOperationException:'无法跟踪'T1Item'类型的实例,因为它没有主键。 Only entity types with primary keys may be tracked.'只能跟踪具有主键的实体类型。
Here is my code:这是我的代码:
using (var scope = scopeFactory.CreateScope())
{
var db1 = scope.ServiceProvider.GetRequiredService<db1Context>();
var db2 = scope.ServiceProvider.GetRequiredService<db2Context>();
var stagingTableConfig = db1 .KreTableFlagConfiguration.Where(x => x.TableName.ToLower() == "kre_productcategory").FirstOrDefault();
if (stagingTableConfig != null)
{
var listProductCategory = db1.KreProductCategory.Where(x => x.Id > stagingTableConfig.LastataIndex).ToList();
if (listProductCategory.Count > 0)
{
long lastIndex = listProductCategory.Select(x => x.Id).Max();
stagingTableConfig.LastataIndex = lastIndex;
List<T1Item> listT1Items = new List<T1Item>();
foreach (var pc in listProductCategory)
{
T1Item t1Item = new T1Item()
{
TiKd = pc.Category,
TiNm = pc.Name,
FlagApproval = 0,
FlagCleansing = 0,
TiKet = pc.Description
};
listT1Items.Add(t1Item);
}
db2.T1Item.AddRange(listT1Items);
db2.SaveChanges();
db1.KreTableFlagConfiguration.Update(stagingTableConfig);
db2.SaveChanges();
}
}
}
I wonder if there is a way I can insert it using ef core.我想知道是否有一种方法可以使用 ef core 插入它。 Maybe there is another option to using ADO net to insert into a table that using PK.也许还有另一种选择使用 ADO 网络插入到使用 PK 的表中。 But if I can use EF core I prefer using EF Core.但如果我可以使用 EF Core,我更喜欢使用 EF Core。
Edit编辑
modelBuilder.Entity<T1Item>(entity =>
{
entity.HasNoKey();
entity.ToTable("T1_ITEM");
entity.Property(e => e.FlagApproval)
.HasColumnName("FLAG_APPROVAL")
.HasColumnType("numeric(5, 0)");
entity.Property(e => e.FlagCleansing)
.HasColumnName("FLAG_CLEANSING")
.HasColumnType("numeric(5, 0)");
entity.Property(e => e.ItPk)
.HasColumnName("IT_PK")
.HasColumnType("numeric(10, 0)");
entity.Property(e => e.TiKd)
.HasColumnName("TI_KD")
.HasMaxLength(5)
.IsUnicode(false)
.IsFixedLength();
entity.Property(e => e.TiKet)
.HasColumnName("TI_KET")
.HasMaxLength(255)
.IsUnicode(false);
entity.Property(e => e.TiNm)
.HasColumnName("TI_NM")
.HasMaxLength(255)
.IsUnicode(false);
});
You can use Keyless
attribute on youe model or use HasNoKey
fluent api in ef core like this:您可以在您的 model 上使用Keyless
属性,或者在 ef 核心中使用HasNoKey
fluent api,如下所示:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<BlogPostsCount>(
eb =>
{
eb.HasNoKey();
eb.Property(v => v.BlogName).HasColumnName("Name");
});
}
Or for use attribute:或用于使用属性:
[Keyless]
public class BlogPostsCount
{
public string BlogName { get; set; }
public int PostCount { get; set; }
}
However, they are different from regular entity types in that they:但是,它们与常规实体类型的不同之处在于:
1- Cannot have a key defined. 1-不能定义一个键。
2- Are never tracked for changes in the DbContext and therefore are never inserted, updated or deleted on the database. 2-永远不会跟踪 DbContext 中的更改,因此永远不会在数据库中插入、更新或删除。
3- Are never discovered by convention. 3-从未按照惯例被发现。 Only support a subset of navigation mapping capabilities, specifically: They may never act as the principal end of a relationship.仅支持导航映射功能的子集,具体而言:它们可能永远不会充当关系的主要端。
4- They may not have navigations to owned entities They can only contain reference navigation properties pointing to regular entities. 4-他们可能没有到拥有实体的导航 他们只能包含指向常规实体的参考导航属性。
5- Entities cannot contain navigation properties to keyless entity types. 5-实体不能包含无键实体类型的导航属性。
6- May be mapped to a defining query. 6-可以映射到定义查询。 A defining query is a query declared in the model that acts as a data source for a keyless entity type.定义查询是在 model 中声明的查询,它充当无键实体类型的数据源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.