2017-06-06 6 views
0

現在、私はエンティティからのデータをsqlite DBに格納する必要があるため、顧客プロジェクトを進めています。私はEF6で作業しています。私は「コードファースト」アプローチを選択しました。Sqlite DBにSystem.Drawing.PointFを格納する最初のアプローチ

問題点:Booの単一の点として、そしてLooに点のリストとしての地理座標をSystem.Drawing.PointFとしてDBに保存したいとします。それは基本的なタイプではないので、残念ながら私が教えたように働いていません。

質問エンティティの定義および/またはモデルの構成で、単一ポイントof Booとポイントリストof LooをDBに保存するにはどうすればよいですか?前もって感謝します!

私のエンティティクラス:

public class Collection 
{ 
    [Key] 
    public int Id { get; set; } 
    public virtual List<Foo> Foos { get; set; } 
} 

public class Foo 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public virtual Collection FooCollection; 
} 

public class Boo : Foo 
{ 
    public PointF Location { get; set; } 
} 

public class Loo : Foo 
{ 
    public List<PointF> Line { get; set; } 
} 

私のモデル構成:

public class ModelConfiguration 
{ 
    public static void Configure(DbModelBuilder modelBuilder) 
    { 
     ConfigureFooCollectionEntity(modelBuilder); 
    } 

    private static void ConfigureFooCollectionEntity(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Collection>().ToTable("base.MyTable") 
      .HasRequired(t => t.Foos) 
      .WithMany() 
      .WillCascadeOnDelete(false); 
    } 

    private static void ConfigureGridElementEntity(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Foo>() 
      .HasRequired(p => p.FooCollection) 
      .WithMany(fooCollection => fooCollection.Foos) 
      .WillCascadeOnDelete(true); 
    } 
} 

マイDbContext:

public class DbContext : DbContext 
{ 
    public DbSet<Collection> DataCollection { get; set; } 

    public DbContext(string nameOrConnectionString) 
     : base(nameOrConnectionString) 
    { 
     Configure(); 
    } 

    public DbContext(DbConnection connection, bool contextOwnsConnection) 
     : base(connection, contextOwnsConnection) 
    { 
     Configure(); 
    } 

    private void Configure() 
    { 
     Configuration.ProxyCreationEnabled = true; 
     Configuration.LazyLoadingEnabled = true; 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     ModelConfiguration.Configure(modelBuilder); 
     var initializer = new DbInitializer(modelBuilder); 
     Database.SetInitializer(initializer); 
    } 
} 

マイDbInitializer:

class DbInitializer : 
SqliteDropCreateDatabaseWhenModelChanges<DbContext> 
{ 
    public GDbInitializer(DbModelBuilder modelBuilder) 
     : base(modelBuilder, typeof(CustomHistory)) 
    { } 
} 
+0

私はあなたがこれまで一緒に良い方法だと思います。 (ただし、 'system.drawing.pointf'はおそらく地理的データには適していないと付け加えるかもしれませんが、EF5以降ではこの種のサポートがあります; https://msdn.microsoft.com/en-US/データ/ hh859721 –

答えて

0

この問題を解決する最も簡単な方法は、System.Drawing.PointFではなく独自のポイントタイプで作業することです。この方法でIDプロパティを与えることができ、EFはそれを別のDBテーブルとして格納することができます。

これは私のためにどのように動作するかです:

[TypeConverter(typeof(ExpandableObjectConverter))] 
public class GeoPoint 
{ 
    [Key] 
    public int Id { get; set; } 
    public float X { get; set; } 
    public float Y { get; set; } 

    [NotMapped] 
    public PointF GetPointF { get { return new PointF(X, Y); } } 

    public GeoPoint() 
    { 
     X = 0; Y = 0; 
    } 

    public GeoPoint(float x, float y) 
    { 
     X = x; Y = y; 
    } 

    public GeoPoint(PointF point) 
    { 
     X = point.X; 
     Y = point.Y; 
    } 

} 
関連する問題