0

さて、ここにいくつかのコンテキストがあります。私はEF5、MVC4、およびSQLCE4を使用してWebアプリケーションを構築しています。私はこのチュートリアルを大まかにフォローしていますが、いくつかの違いがあります。

  1. 私のコンテキストクラスとPOCOオブジェクトは独自のアセンブリにあります。
  2. SQLExpressローカルDBの代わりにSQLCE4を使用しています
  3. 私のクラスはチュートリアルほど単純ではありません

私はすでに回避策を使用して、単純なクラスを機能登録させています。

EF5で列挙型を使用することはEF5でサポートされていると思っていましたが、キーで使用できますか?

単純なクラス(1つのintキープロパティ、1つの文字列プロパティ)のコントロール(Add Controller、読み取り/書き込みアクションとビューを備えたMVCコントローラー、Entity Frameworkを使用)を追加しようとすると、機能します。キーの一部であるプロパティ(プライマリまたは外部)を持つクラスを追加しようとすると、さまざまなエラーが発生します

Unable to retrieve metadata for 'blah'.  Using the 
same DbCompiledModel to create contexts against different types of 
database servers is not supported. Instead, create a separate 
DbCompiledModel for each type of server being used.

Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

C:\Program Files (x86)\Microsoft Visual Studio\11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\
CodeTemplates\AddController\ControllerWithContext.tt(0,0) : error :
Running transformation: System.IndexOutOfRangeException: Index was outside the bounds of the
array.
---StackTrace---

これらのクラス間で私が見つけた唯一の類似点は、キーに関連付けられたエムンがあることです。非キー列挙型を持つ他のクラスは正しく生成されます。これは問題ですか、それとも私は完全にマークを逃しましたか?

編集:失敗するクラスの例

public class A
{
    public virtual AIdEnum Id { get; set; }
    public virtual string Name { get; set; }

    public virtual ICollection<B> Bs { get; set; }
    public virtual ICollection<C> Cs { get; set; }
}
4

3 に答える 3

0

私の問題は、コントローラーの作成がSQLCE 4.0 connectionStringsで機能しないため、System.Data.SqlClientのプロバイダーのconactionStringを使用してその問題を処理したことです。

私が抱えていた次の問題は、connectionString 値 (暗号化など) がこの方法では尊重されなかったため、このバグを回避するために 2 つの異なるコンストラクターが必要になったことです。

#if DEBUG
    public Context()
        : base("name=DefaultConnection")
    { ; }
#else
    /// <summary>
    /// Default Constructor
    /// </summary>
    public Context()
        : base("name=CompactConnection")
    { ; }
#endif

これが私の構成です:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="System.Data.SqlServerCe.4.0" />
        </parameters>
    </defaultConnectionFactory>
</entityFramework>
<connectionStrings>
    <add name="CompactConnection" providerName="System.Data.SqlServerCe.4.0"
        connectionString="Data Source=&quot;|DataDirectory|\DB.sdf&quot;;encryption mode=platform default;password=&quot;P4$$w0rd!&quot;"/>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MsSqlCe-20121028004432;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MsSqlCe-20121028004432.mdf" />
</connectionStrings>

もちろん、これはより深い問題の回避策にすぎません。他の誰かがこの問題の根本的な原因を知っている場合は、ぜひ知りたいです.

于 2013-02-08T19:31:27.317 に答える