2017-06-06 51 views
0

我目前正在一個客戶項目上工作,我必須將實體中的數據存儲在sqlite數據庫中。我正在使用EF6,我選擇了「代碼優先」方法。代碼第一種方法來存儲Sqlite數據庫中的System.Drawing.PointF

問題:我想用來存儲地理座標(如單點Boo和點的Loo列表)作爲DB System.Drawing.PointF。因爲它不是一個原始類型,所以不幸的是,它不像我教的那樣工作。

問題:在我的實體定義和/或我的模型配置中,如何更改數據庫中的單個點Boo以及點的列表Loo?先謝謝你!

我的實體類:

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; 
    } 

} 
相關問題