簡體   English   中英

EF5代碼優先遷移-創建數據庫和遷移錯誤

[英]EF5 Code First Migration - Creating database and migration error

我最近參加了另一個同事的CF5項目,我想添加一個新功能,但是有一些錯誤。 因此,我想為此應用程序創建一個自動創建和自動遷移系統。

我添加了它來檢查app.cs構造函數中的數據庫狀態:

using (var db = new DatabaseContext())
        {
            do
            {
                Cursor.Current = Cursors.WaitCursor;
                // If DB already exists, tests his integrity
                if (db.Database.Exists() == true)
                {
                    bool isCompatible = false;
                    bool isMetadataMissing = false;

                    // Tests compatibility between DB and EF model
                    try
                    {
                        isCompatible = db.Database.CompatibleWithModel(true);
                    }
                    catch (Exception e)
                    {
                        // Exception has thrown because the database don't contains metadata informations
                        isCompatible = false;
                        isMetadataMissing = true;
                        m_Logger.Error("Error during checking compatibility of database : " + e.Message, e);
                    }

                    // If database is compatible, we quit de tests
                    if (isCompatible)
                    {
                        result = true;
                        isWorkRequired = false;
                        m_Logger.Debug("Database Ok");
                        Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        // If DB isn't compatible and metadata was found on database, we proceed the migration of database
                        if (!isMetadataMissing)
                        {
                            try
                            {
                                m_Logger.Debug("Migration needed on database");
                                db.Database.Initialize(true);
                            }
                            catch (Exception eMig)
                            {
                                result = false;
                                isWorkRequired = false;
                                m_Logger.Debug("Can't migrates database. "+eMig.Message);
                                Cursor.Current = Cursors.Default;
                                messageBoxViewModel = new MessageBoxViewModel(Resources.Resources.ErrorDBMetaError,
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                                messageBoxView = new MessageBoxView(messageBoxViewModel);
                                messageBoxView.ShowDialog();
                            }
                        }
                        else // Warns the user that there is a problem with the ConnexionString
                        {
                            result = false;
                            isWorkRequired = false;
                            m_Logger.Debug("Can't update database, the metadata is missing");
                            Cursor.Current = Cursors.Default;
                            messageBoxViewModel = new MessageBoxViewModel(Resources.Resources.ErrorDBMetaMissing, 
                                MessageBoxButton.OK,MessageBoxImage.Error);
                            messageBoxView = new MessageBoxView(messageBoxViewModel);
                            messageBoxView.ShowDialog();
                        }
                    }
                }
                else
                {
                    m_Logger.Debug("Database is Mising");
                    Cursor.Current = Cursors.Default;
                    messageBoxViewModel = new MessageBoxViewModel(Resources.Resources.QtCreateDB, 
                        MessageBoxButton.YesNo,MessageBoxImage.Question);
                    messageBoxView = new MessageBoxView(messageBoxViewModel);
                    messageBoxView.ShowDialog();

                    if (messageBoxViewModel.Result == MessageBoxResult.Yes)
                    {
                        Cursor.Current = Cursors.WaitCursor;
                        m_Logger.Debug("Creating database");
                        db.Database.Create();
                        //db.Database.Initialize(true);

                        m_Logger.Debug("Adds roles objects");
                        // Adds initial data

                        m_Logger.Debug("Database created");
                    }
                    else
                    {
                        isWorkRequired = false;
                    }
                }
            } while (isWorkRequired);
        }

此后,已經創建了數據庫,但是沒有應用以前的遷移:-s因此,如果我要執行“添加遷移”,控制台會告訴我有些遷移正在等待更改。

因此,我手動執行“ update-database”,但出現此錯誤:“每個表中的列名必須是唯一的。表“ dbo.Segment”中的列名“ TrackName”被多次指定”

為什么在創建數據庫時未應用遷移?

誰能幫我 ? 我是CF5的新手:-)謝謝,

Segment.CS:

  public class Segment
{
    public System.Guid Id { get; set; }
    public System.Guid NetworkId { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public int SourceIndex { get; set; }
    public int TargetIndex { get; set; }
    public bool IsInitialized { get; set; }
    public int NeighborNodeDistance { get; set; }
    public string TrackName { get; set; }
    public bool IsTrackNameFixed { get; set; }
    public byte[] GeometryParameters { get; set; }
    public Nullable<System.Guid> SourceNodeId { get; set; }
    public Nullable<System.Guid> TargetNodeId { get; set; }
    public double Length { get; set; }
    public virtual Network Network { get; set; }
}

SegmentMap.CS:

  public class SegmentMap : EntityTypeConfiguration<Segment>
{
    public SegmentMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Type)
            .HasMaxLength(255);

        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(50);

      this.Property(t => t.GeometryParameters)
        .IsRequired()
        .HasMaxLength(512);

        // Table & Column Mappings
        this.ToTable("Segment");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.NetworkId).HasColumnName("NetworkId");
        this.Property(t => t.Type).HasColumnName("Type");
        this.Property(t => t.Name).HasColumnName("Name");
        this.Property(t => t.SourceIndex).HasColumnName("SourceIndex");
        this.Property(t => t.TargetIndex).HasColumnName("TargetIndex");
        this.Property(t => t.IsInitialized).HasColumnName("IsInitialized");
        this.Property(t => t.NeighborNodeDistance).HasColumnName("NeighborNodeDistance");
        this.Property(t => t.GeometryParameters).HasColumnName("GeometryParameters");
        this.Property(t => t.SourceNodeId).HasColumnName("SourceNodeId");
        this.Property(t => t.TargetNodeId).HasColumnName("TargetNodeId");
        this.Property(t => t.Length).HasColumnName("Length");
        this.Property(t => t.TrackName).HasColumnName("TrackName");
        this.Property(t => t.IsTrackNameFixed).HasColumnName("IsTrackNameFixed");

        // Relationships
        this.HasRequired(t => t.Network)
            .WithMany(t => t.Segments)
            .HasForeignKey(d => d.NetworkId);

    }
}

Configuration.CS:

using System.Data.Entity.Migrations;
internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = false;
    }

    protected override void Seed(DatabaseContext context)
    {

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM