使用 Entity Framework Code First 在数据库中存储 System.Version
Storing System.Version in database with Entity Framework Code First
我有一个使用 Entity Framework 6.1 代码创建的数据库 首先,我有一个特定的 table 我遇到了问题。
我有一个table Manifests,实体定义如下
public class Manifest
{
public int Id { get; set; }
public DateTime ReleaseDate { get; set; }
public int VersionMajor { get; set; }
public int VersionMinor { get; set; }
public int VersionBuild { get; set; }
public int VersionRevision { get; set; }
public Version Version
{
get
{
return new Version(this.VersionMajor, this.VersionMinor, this.VersionBuild, this.VersionRevision);
}
set
{
this.VersionMajor = value.Major;
this.VersionMinor = value.Minor;
this.VersionBuild = value.Build;
this.VersionRevision = value.Revision;
}
}
public string ReleaseNotes { get; set; }
public bool IsCompulsory { get; set; }
public string Description { get; set; }
public string UpdatePackage { get; set; }
}
你会注意到我定义了一个 属性 ,它是一个 System.Version
并将存储清单版本,但是,当我 运行 在包管理器控制台中添加迁移时生成的迁移不包括 Version
列。我已经尝试在 OnModelCreating
中设置 modelBuilder.ComplexType<System.Version>()
但无济于事,请帮忙?
按照建议,我已经更改了实体定义(见上文),但是现在 运行ing 种子时出现错误,请见下文:
System.Data.Entity.Core.EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details. ---> System.Data.Entity.Core.MappingException:
(6,10) : error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties Manifest.Version in Set Manifests.
An Entity with Key (PK) will not round-trip when:
Entity is type [Drogo.Meera.Data.Context.Manifest]
种子方法有以下:
context.Manifests.AddOrUpdate(
p => p.Version,
new Manifest
{
Description = "Initial Release",
VersionMajor = 1,
VersionMinor = 0,
VersionBuild = 0,
IsCompulsory = true,
ReleaseDate = DateTime.Now,
ReleaseNotes = "Initial Release",
UpdatePackage = @"v1-0-0\v1-0-0.zip"
});
context.SaveChanges();
我认为当您将 System.Version
用作复杂类型时,您没有看到预期的结果,因为它没有可供 EF 使用的任何有效属性。
If a property has only a getter or a setter, but not both, it will not be included in the model.
您应该能够解决这个问题,方法是提取您想要自己保留的属性(并将 Manifest.Version
标记为要忽略的 属性)。
public class Manifest
{
// other properties
int VersionMajor { get; set; }
int VersionMinor { get; set; }
int VersionBuild { get; set; }
int VersionRevision { get; set; }
public Version Version
{
get
{
return new Version(VersionMajor, VersionMinor, VersionBuild, VersionRevision);
}
set
{
VersionMajor = value.Major;
VersionMinor = value.Minor;
VersionBuild = value.Build;
VersionRevision = value.Revision;
}
}
}
// Adding [NotMapped] to Manifest.Version would also work, as in the link below
modelBuilder.Entity<Manifest>().Ignore(m => m.Version);
这里还有一个例子:Entity Framework map multiple columns to C# complex type
我有一个使用 Entity Framework 6.1 代码创建的数据库 首先,我有一个特定的 table 我遇到了问题。
我有一个table Manifests,实体定义如下
public class Manifest
{
public int Id { get; set; }
public DateTime ReleaseDate { get; set; }
public int VersionMajor { get; set; }
public int VersionMinor { get; set; }
public int VersionBuild { get; set; }
public int VersionRevision { get; set; }
public Version Version
{
get
{
return new Version(this.VersionMajor, this.VersionMinor, this.VersionBuild, this.VersionRevision);
}
set
{
this.VersionMajor = value.Major;
this.VersionMinor = value.Minor;
this.VersionBuild = value.Build;
this.VersionRevision = value.Revision;
}
}
public string ReleaseNotes { get; set; }
public bool IsCompulsory { get; set; }
public string Description { get; set; }
public string UpdatePackage { get; set; }
}
你会注意到我定义了一个 属性 ,它是一个 System.Version
并将存储清单版本,但是,当我 运行 在包管理器控制台中添加迁移时生成的迁移不包括 Version
列。我已经尝试在 OnModelCreating
中设置 modelBuilder.ComplexType<System.Version>()
但无济于事,请帮忙?
按照建议,我已经更改了实体定义(见上文),但是现在 运行ing 种子时出现错误,请见下文:
System.Data.Entity.Core.EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details. ---> System.Data.Entity.Core.MappingException:
(6,10) : error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties Manifest.Version in Set Manifests.
An Entity with Key (PK) will not round-trip when:
Entity is type [Drogo.Meera.Data.Context.Manifest]
种子方法有以下:
context.Manifests.AddOrUpdate(
p => p.Version,
new Manifest
{
Description = "Initial Release",
VersionMajor = 1,
VersionMinor = 0,
VersionBuild = 0,
IsCompulsory = true,
ReleaseDate = DateTime.Now,
ReleaseNotes = "Initial Release",
UpdatePackage = @"v1-0-0\v1-0-0.zip"
});
context.SaveChanges();
我认为当您将 System.Version
用作复杂类型时,您没有看到预期的结果,因为它没有可供 EF 使用的任何有效属性。
If a property has only a getter or a setter, but not both, it will not be included in the model.
您应该能够解决这个问题,方法是提取您想要自己保留的属性(并将 Manifest.Version
标记为要忽略的 属性)。
public class Manifest
{
// other properties
int VersionMajor { get; set; }
int VersionMinor { get; set; }
int VersionBuild { get; set; }
int VersionRevision { get; set; }
public Version Version
{
get
{
return new Version(VersionMajor, VersionMinor, VersionBuild, VersionRevision);
}
set
{
VersionMajor = value.Major;
VersionMinor = value.Minor;
VersionBuild = value.Build;
VersionRevision = value.Revision;
}
}
}
// Adding [NotMapped] to Manifest.Version would also work, as in the link below
modelBuilder.Entity<Manifest>().Ignore(m => m.Version);
这里还有一个例子:Entity Framework map multiple columns to C# complex type