我不知道它是实体框架的设计选择还是代表我的错误方法,但每当我尝试将AddRange实体添加到DbSet时,我似乎无法获得自动生成的IDENTITY字段.
[Table("entities")]
public class Entity
{
[Key]
[Column("id")]
public long Id { get; set; }
[Column("field")]
public string Field { get; set; }
}
var entities = new Entity[]
{
new Entity() { Field = "A" },
new Entity() { Field = "B" },
};
_dbContext.Entities.AddRange(entities);
await _dbContext.SaveChangesAsync();
//ids are still default(long) at this point!!
Run Code Online (Sandbox Code Playgroud)
编辑: 这是更新的代码,以显示导致问题的原因:枚举.无需向实体类添加其他属性.
public class Request
{
public string Field { get; set; }
public Entity ToEntity()
{
return new Entity() { Field = Field }; …Run Code Online (Sandbox Code Playgroud) 我编写了一个通用管道,它接受多个参数,以便将版本从预定义的 GitHub 存储库部署到特定节点。我想在 GitHub 上的 Jenkinsfile 上托管此管道,因此我将作业配置为使用“来自 SCM 的管道脚本”。事实是 - 当我尝试构建作业时 - Jenkinsfile 在每个节点上都会被检查。是否可以仅在主节点上签出并执行 Jenkinsfile 并按预期运行管道?
编辑:正如我之前所说,管道工作得很好,并且按预期将作业设置为与管道脚本一起使用。问题是,当我尝试将其更改为“来自 SCM 的管道脚本”时,Jenkinsfile 会在每个代理上进行检出,这是一个问题,因为除了 master 之外,我没有在任何代理上安装 git。我希望 Jenkinsfile 仅在主代理上签出并按预期执行。仅供参考以下管道:
def agents = "$AGENTS".toString()
def agentLabel = "${ println 'Agents: ' + agents; return agents; }"
pipeline {
agent none
stages {
stage('Prep') {
steps {
script {
if (agents == null || agents == "") {
println "Skipping build"
skipBuild = true
}
if (!skipBuild) {
println "Agents set for this build: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码来获取IEntityModelBuilder的所有实现,但它将返回一个空集合。
public class EntityFrameworkDbContext : DbContext
{
//constructor(s) and entities DbSets...
private static IEnumerable<IEntityModelBuilder<IEntity>> _entitymodelBuilders;
internal IEnumerable<IEntityModelBuilder<IEntity>> EntityModelBuilders
{
get
{
if (_entitymodelBuilders == null)
{
var type = typeof(IEntityModelBuilder<IEntity>);
_entitymodelBuilders = Assembly.GetAssembly(type).GetTypes()
.Where(t => type.IsAssignableFrom(t) && t.IsClass)
.Select(t => (IEntityModelBuilder<IEntity>)Activator.CreateInstance(t, new object[0]));
}
return _entitymodelBuilders;
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (var builder in EntityModelBuilders)
builder.Build(modelBuilder);
base.OnModelCreating(modelBuilder);
}
}
internal interface IEntityModelBuilder<TEntity> where TEntity : IEntity
{
void Build(DbModelBuilder modelBuilder);
}
//sample implementation
internal class UserModelBuilder …Run Code Online (Sandbox Code Playgroud)