如何在SQL Server中存储C#的复杂结构。

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

How to store a C# Complex struct in SQL Server

问题

我在C#类中有一个复数:

``` csharp
public class DataToStore
{
  [Key]
  public int Id { get; set; }
  public System.Numerics.Complex ComplexValue { get; set; }
}

我需要将这个数据存储在SQL Server数据库中,但是我不知道在SQL中等价的数据类型是什么。

目前,我正在尝试使用Entity Framework Core生成数据库架构,但是在创建迁移时,EF报错说它无法映射这种类型:

> 无法映射属性 'DataToStore.ComplexValue',因为它的类型是 'Complex',这不是受支持的原始类型或有效的实体类型。要么显式地映射此属性,要么使用 '[NotMapped]' 属性或在 'OnModelCreating' 中使用 'EntityTypeBuilder.Ignore' 忽略它。

这是有道理的,但是我不知道如何在SQL中表示这种数据。

我不一定非要使用Entity Framework - 我只是用它来快速模拟数据库 - 所以如果在SQL中可以手动完成而EF无法做到的话,那也没问题。我的目标是将这些数据存储在数据库中,并且能够以完全相同的格式再次取出。

在SQL中存储这种类型的数据的最佳方法是什么?或者这个值需要转换成其他类型才能存储?


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

I have a Complex number in a C# class

``` csharp
public class DataToStore
{
  [Key]
  public int Id { get; set; }
  public System.Numerics.Complex ComplexValue { get; set; }
}

I need to store this data in a SQL Server database, but I don't know what the equivalent type is in SQL.

For now, I'm trying to generate the database schema using Entity Framework Core, but when creating the migration EF is complaining that it can't map this type:

> The property 'DataToStore.ComplexValue' could not be mapped because it
> is of type 'Complex', which is not a supported primitive type or a
> valid entity type. Either explicitly map this property, or ignore it
> using the '[NotMapped]' attribute or by using
> 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

Which makes sense, but I don't know how this data can be represented in SQL.

I'm not specifically tied to using Entity Framework - I'm only using this as a way to quickly mock up the database - so if this is something that can be done manually in SQL but not EF, then that's absolutely fine. My aim here is to get this data in the database and be able to get it out again in exactly the same format.

What's the best way of storing this type of data in SQL? Or does this value need converting to another type so that it can be stored?

答案1

得分: 3

你需要多个数据库字段来存储它的值。如果要存储RealImaginary的值,你可以为每个值创建一个字段。

你需要更新你的模型,以便EF不尝试保存ComplexValue。你应该为实部和虚部值创建属性,并使用它们来构造你的复数值。

public class DataToStore
{
    [Key]
    public int Id { get; set; }

    [NotMapped]
    public System.Numerics.Complex ComplexValue 
    { 
        get
        {
            return new Complex(Real, Imaginary);
        }
        set
        {
            Real = value.Real; 
            Imaginary = value.Imaginary;
        }
    }

    public double Real { get; set; }
    
    public double Imaginary { get; set; }
}
英文:

You would need multiple database fields to store its values. If you want to store the Real and Imaginary values, you could have a field for each of them.

You would need to update your model so that EF does not try to save ComplexValue. You should have properties for the real and imaginary values and use them to make your complex value.

public class DataToStore
{
    [Key]
    public int Id { get; set; }

    [NotMapped]
    public System.Numerics.Complex ComplexValue 
	{ 
		get
		{
			return new Complex(Real, Imaginary)
		}
		set
		{
			Real = value.Real; 
			Imaginary = value.Imaginary;
		}
	}

    public double Real { get; set; }
    
    public double Imaginary { get; set; }
}

huangapple
  • 本文由 发表于 2023年2月8日 20:01:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385519.html
匿名

发表评论

匿名网友

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

确定