EF Core将对象映射到多个数据库

huangapple go评论74阅读模式
英文:

EF Core map object to multiple databases

问题

我正在.NET Core中构建一个对象,将使用EF Core映射到数据库。然而,这个对象的一个属性需要映射到一个单独的只读数据库。

我指的是这样的:

public class Sample
{
    public int Id { get; set; }
    [Required]
    public Location Location { get; set; }
    public string SampleValue { get; set; }
}

[Keyless]
public class Location
{
    [Column("LOCATION_ID")]
    public int Id { get; set; }
    [Column("LOCATION_DESC")]
    public string Name { get; set; }
}

Location 对象是使用预定义的 SQL 查询从只读的 Oracle 数据库中提取的,并使用 FromSQL 方法检索的,这就是为什么我有 [Keyless] 属性:

_dbContext.Locations.FromSql<Location>(query)

Sample 将存储在一个 SQL 数据库中,位置将与 Location ID 一起存储在该数据库中,因此当我检索 Sample 对象时,EF Core 将获取 ID 并从 Oracle 数据库中获取它,然后创建正确的 Location 属性并进行映射。

英文:

I am building an object in .NET Core that will be mapped to a database using EF Core. However one of the properties of this object will need to be mapped to a separate read only database.

What I mean is this:

public class Sample
{
	public int Id { get; set; }
	[Required]
	public Location Location { get; set; }
    public string SampleValue { get; set; }
}

[Keyless]
public class Location
{
    [Column(&quot;LOCATION_ID&quot;)]
    public int Id { get; set; }
    [Column(&quot;LOCATION_DESC&quot;)]
    public string Name { get; set; }
}

The Location object is pulled from a read only Oracle database using a predefined SQL query and retrieved using the FromSQL method, which is why I have the [Keyless] attribute:

_dbContext.Locations.FromSql&lt;Location&gt;(query)

The Sample will be stored in a SQL database and the location would be stored in that database with the Location ID, so when I retrieve a Sample object, EF Core will pick up the ID and get it from the Oracle database and create the correct Location property and map it

答案1

得分: 2

抱歉,你将无法填充 _dbContext.Locations.FromSql&lt;Location&gt;(query),因为你需要连接到不同的数据库。

DbContext 不用于管理两个数据库的状态。如果你的领域实体(Sample)来自两个不同的数据源,我建议不要依赖 ORM 框架,而是自己构建实体。

var dbSample = _dbContext1.DbSample.Get();
var dbLocation = _dbLocationContext.DbLocation.Get(dbSample.locationId);

return SampleBuilder.Build(dbSample, dbLocation);

这种方法的缺点是你将不得不实现从领域实体到数据库表示的映射。但这种方法更直接,而且实现和维护都相当容易。

英文:

Unfortunately you won't be able to populate _dbContext.Locations.FromSql&lt;Location&gt;(query), because you would need to connect to different database.

DbContext is not used for managing state of 2 databases. If your domain entity (Sample) created from 2 different data sources, I would recommend not relying on ORM Framework and build the entity yourself.

var dbSample = _dbContext1.DbSample.Get();
var dbLocation = _dbLocationContext.DbLocation.Get(dbSample.locationId);

return SampleBuilder.Build(dbSample, dbLocation);

Downside of this approach is that you will have to implement mappings from Domain Entity to Database representation.
But this approach is more straight forward and quite easy to implement and maintain.

答案2

得分: 0

所以,根据 @vshishkarov 的建议,我决定执行以下操作:

public class Sample
{
public int Id { get; set; }
[Required]
public string LocationId { get; set; }
[NotMapped]
public Location Location { get; set; }
public string SampleValue { get; set; }
}

[Keyless]
public class Location
{
[Column("LOCATION_ID")]
public int Id { get; set; }
[Column("LOCATION_DESC")]
public string Name { get; set; }
}

var dbSample = _dbContext1.DbSample.Get();
var dbLocation = _dbLocationContext.DbLocation.Get(dbSample.locationId);

dbSample.Location = dbLocation;
return dbSample;


<details>
<summary>英文:</summary>

So following what @vshishkarov I have decided to do: 

public class Sample
{
public int Id { get; set; }
[Required]
public string LocationId { get; set; }
[NotMapped]
public Location Location { get; set; }
public string SampleValue { get; set; }
}

[Keyless]
public class Location
{
[Column("LOCATION_ID")]
public int Id { get; set; }
[Column("LOCATION_DESC")]
public string Name { get; set; }
}

var dbSample = _dbContext1.DbSample.Get();
var dbLocation = _dbLocationContext.DbLocation.Get(dbSample.locationId);

dbSample.Location = dbLocation;
return dbSample;

huangapple
  • 本文由 发表于 2023年2月23日 22:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定