none
Creating a constraint in the Entity Framework model where none exist in the database. RRS feed

  • Question

  • I have set up the following query/model that works fairly well:

                PurchaseOrder order = (from o in db.PurchaseOrders where o.OrderGroupId == orderGroupId select o).FirstOrDefault();
    

    The problem is that the PurchaseOrder "model" has some navigational properties like:

        /// <summary>
        /// There are no comments for OrderAddresses in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("Buyseasons_transactionsModel", "FK_OrderAddresses_PurchaseOrders", "OrderAddresses")]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        [global::System.Xml.Serialization.XmlIgnoreAttribute()]
        [global::System.Xml.Serialization.SoapIgnoreAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public global::System.Data.Objects.DataClasses.EntityCollection<OrderAddress> OrderAddresses
        {
          get
          {
            return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<OrderAddress>("Buyseasons_transactionsModel.FK_OrderAddresses_PurchaseOrders", "OrderAddresses");
          }
          set
          {
            if ((value != null))
            {
              ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<OrderAddress>("Buyseasons_transactionsModel.FK_OrderAddresses_PurchaseOrders", "OrderAddresses", value);
            }
          }
        }
    
    

    When I do the query above the object that is returned always has a collection that is always empty. I suspect that it is because there is no constraint in the database. Any comments on how I can add the constraint to the model when it is missing from the database?

     


    Kevin Burton
    • Moved by VMazurModerator Wednesday, August 10, 2011 10:36 AM (From:ADO.NET Managed Providers)
    Monday, August 8, 2011 6:56 PM

Answers

  • Hi Kevin,

    >>I am assuming that I can use EF 4.1 with an existing database?

    The Answer is Yes, you just need to point Code First to an existing database is to add a App/Web.config connection string, please refer this link.

    >> Why is the check box to include FK when building from the database disabled? 

    Please try to change your project Target Framework to .Net 4.0

    In order to load the related collections(Navigation), you can use .Include()--(the same with EF4.1), in EF4.1 you can refer here

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by KevinBurton Thursday, August 25, 2011 1:02 PM
    Thursday, August 25, 2011 10:05 AM
    Moderator

All replies

  • Or is it because Lazy loading is on?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, August 8, 2011 7:55 PM
  • The problem that I have with this article is probably becuase I am relatively new to EntityFramework. The article references properties or variables that I don't see a definition for like 'Data' or 'SupplierReference'. How would I modify the code generated by the designer to load the object when required?

     


    Kevin Burton
    Monday, August 8, 2011 8:44 PM
  • is there a OrderAddressesReference property in your class?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, August 8, 2011 8:52 PM
  • The designer did not create one. Just OrderAddresses as shown above.

     

    Sorry if it is a bit verbose but here is the class that the designer built:

      /// <summary>
      /// There are no comments for Buyseasons_transactionsModel.PurchaseOrder in the schema.
      /// </summary>
      /// <KeyProperties>
      /// OrderGroupId
      /// </KeyProperties>
      [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="Buyseasons_transactionsModel", Name="PurchaseOrder")]
      [global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
      [global::System.Serializable()]
      public partial class PurchaseOrder : global::System.Data.Objects.DataClasses.EntityObject
      {
        /// <summary>
        /// Create a new PurchaseOrder object.
        /// </summary>
        /// <param name="orderGroupId">Initial value of OrderGroupId.</param>
        /// <param name="soldToId">Initial value of SoldToId.</param>
        /// <param name="lineItemCount">Initial value of LineItemCount.</param>
        /// <param name="shippingTotal">Initial value of ShippingTotal.</param>
        /// <param name="handlingTotal">Initial value of HandlingTotal.</param>
        /// <param name="taxTotal">Initial value of TaxTotal.</param>
        /// <param name="subTotal">Initial value of SubTotal.</param>
        /// <param name="total">Initial value of Total.</param>
        /// <param name="created">Initial value of Created.</param>
        /// <param name="lastModified">Initial value of LastModified.</param>
        /// <param name="modifiedBy">Initial value of ModifiedBy.</param>
        /// <param name="basketId">Initial value of BasketId.</param>
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public static PurchaseOrder CreatePurchaseOrder(global::System.Guid orderGroupId, global::System.Guid soldToId, int lineItemCount, decimal shippingTotal, decimal handlingTotal, decimal taxTotal, decimal subTotal, decimal total, global::System.DateTime created, global::System.DateTime lastModified, string modifiedBy, global::System.Guid basketId)
        {
          PurchaseOrder purchaseOrder = new PurchaseOrder();
          purchaseOrder.OrderGroupId = orderGroupId;
          purchaseOrder.SoldToId = soldToId;
          purchaseOrder.LineItemCount = lineItemCount;
          purchaseOrder.ShippingTotal = shippingTotal;
          purchaseOrder.HandlingTotal = handlingTotal;
          purchaseOrder.TaxTotal = taxTotal;
          purchaseOrder.SubTotal = subTotal;
          purchaseOrder.Total = total;
          purchaseOrder.Created = created;
          purchaseOrder.LastModified = lastModified;
          purchaseOrder.ModifiedBy = modifiedBy;
          purchaseOrder.BasketId = basketId;
          return purchaseOrder;
        }
        /// <summary>
        /// There are no comments for property OrderGroupId in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public global::System.Guid OrderGroupId
        {
          get
          {
            return this._OrderGroupId;
          }
          set
          {
            this.OnOrderGroupIdChanging(value);
            this.ReportPropertyChanging("OrderGroupId");
            this._OrderGroupId = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("OrderGroupId");
            this.OnOrderGroupIdChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private global::System.Guid _OrderGroupId;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnOrderGroupIdChanging(global::System.Guid value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnOrderGroupIdChanged();
        /// <summary>
        /// There are no comments for property Name in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string Name
        {
          get
          {
            return this._Name;
          }
          set
          {
            this.OnNameChanging(value);
            this.ReportPropertyChanging("Name");
            this._Name = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("Name");
            this.OnNameChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _Name;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnNameChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnNameChanged();
        /// <summary>
        /// There are no comments for property SoldToId in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public global::System.Guid SoldToId
        {
          get
          {
            return this._SoldToId;
          }
          set
          {
            this.OnSoldToIdChanging(value);
            this.ReportPropertyChanging("SoldToId");
            this._SoldToId = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("SoldToId");
            this.OnSoldToIdChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private global::System.Guid _SoldToId;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSoldToIdChanging(global::System.Guid value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSoldToIdChanged();
        /// <summary>
        /// There are no comments for property SoldToAddressId in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string SoldToAddressId
        {
          get
          {
            return this._SoldToAddressId;
          }
          set
          {
            this.OnSoldToAddressIdChanging(value);
            this.ReportPropertyChanging("SoldToAddressId");
            this._SoldToAddressId = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("SoldToAddressId");
            this.OnSoldToAddressIdChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _SoldToAddressId;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSoldToAddressIdChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSoldToAddressIdChanged();
        /// <summary>
        /// There are no comments for property LineItemCount in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public int LineItemCount
        {
          get
          {
            return this._LineItemCount;
          }
          set
          {
            this.OnLineItemCountChanging(value);
            this.ReportPropertyChanging("LineItemCount");
            this._LineItemCount = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("LineItemCount");
            this.OnLineItemCountChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private int _LineItemCount;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnLineItemCountChanging(int value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnLineItemCountChanged();
        /// <summary>
        /// There are no comments for property ShippingTotal in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public decimal ShippingTotal
        {
          get
          {
            return this._ShippingTotal;
          }
          set
          {
            this.OnShippingTotalChanging(value);
            this.ReportPropertyChanging("ShippingTotal");
            this._ShippingTotal = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("ShippingTotal");
            this.OnShippingTotalChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private decimal _ShippingTotal;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnShippingTotalChanging(decimal value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnShippingTotalChanged();
        /// <summary>
        /// There are no comments for property HandlingTotal in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public decimal HandlingTotal
        {
          get
          {
            return this._HandlingTotal;
          }
          set
          {
            this.OnHandlingTotalChanging(value);
            this.ReportPropertyChanging("HandlingTotal");
            this._HandlingTotal = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("HandlingTotal");
            this.OnHandlingTotalChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private decimal _HandlingTotal;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnHandlingTotalChanging(decimal value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnHandlingTotalChanged();
        /// <summary>
        /// There are no comments for property TaxTotal in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public decimal TaxTotal
        {
          get
          {
            return this._TaxTotal;
          }
          set
          {
            this.OnTaxTotalChanging(value);
            this.ReportPropertyChanging("TaxTotal");
            this._TaxTotal = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("TaxTotal");
            this.OnTaxTotalChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private decimal _TaxTotal;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnTaxTotalChanging(decimal value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnTaxTotalChanged();
        /// <summary>
        /// There are no comments for property SubTotal in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public decimal SubTotal
        {
          get
          {
            return this._SubTotal;
          }
          set
          {
            this.OnSubTotalChanging(value);
            this.ReportPropertyChanging("SubTotal");
            this._SubTotal = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("SubTotal");
            this.OnSubTotalChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private decimal _SubTotal;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSubTotalChanging(decimal value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSubTotalChanged();
        /// <summary>
        /// There are no comments for property Total in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public decimal Total
        {
          get
          {
            return this._Total;
          }
          set
          {
            this.OnTotalChanging(value);
            this.ReportPropertyChanging("Total");
            this._Total = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("Total");
            this.OnTotalChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private decimal _Total;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnTotalChanging(decimal value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnTotalChanged();
        /// <summary>
        /// There are no comments for property Created in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public global::System.DateTime Created
        {
          get
          {
            return this._Created;
          }
          set
          {
            this.OnCreatedChanging(value);
            this.ReportPropertyChanging("Created");
            this._Created = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("Created");
            this.OnCreatedChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private global::System.DateTime _Created;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnCreatedChanging(global::System.DateTime value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnCreatedChanged();
        /// <summary>
        /// There are no comments for property LastModified in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public global::System.DateTime LastModified
        {
          get
          {
            return this._LastModified;
          }
          set
          {
            this.OnLastModifiedChanging(value);
            this.ReportPropertyChanging("LastModified");
            this._LastModified = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("LastModified");
            this.OnLastModifiedChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private global::System.DateTime _LastModified;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnLastModifiedChanging(global::System.DateTime value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnLastModifiedChanged();
        /// <summary>
        /// There are no comments for property ModifiedBy in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string ModifiedBy
        {
          get
          {
            return this._ModifiedBy;
          }
          set
          {
            this.OnModifiedByChanging(value);
            this.ReportPropertyChanging("ModifiedBy");
            this._ModifiedBy = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);
            this.ReportPropertyChanged("ModifiedBy");
            this.OnModifiedByChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _ModifiedBy;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnModifiedByChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnModifiedByChanged();
        /// <summary>
        /// There are no comments for property TrackingNumber in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string TrackingNumber
        {
          get
          {
            return this._TrackingNumber;
          }
          set
          {
            this.OnTrackingNumberChanging(value);
            this.ReportPropertyChanging("TrackingNumber");
            this._TrackingNumber = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("TrackingNumber");
            this.OnTrackingNumberChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _TrackingNumber;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnTrackingNumberChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnTrackingNumberChanged();
        /// <summary>
        /// There are no comments for property BasketId in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public global::System.Guid BasketId
        {
          get
          {
            return this._BasketId;
          }
          set
          {
            this.OnBasketIdChanging(value);
            this.ReportPropertyChanging("BasketId");
            this._BasketId = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
            this.ReportPropertyChanged("BasketId");
            this.OnBasketIdChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private global::System.Guid _BasketId;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnBasketIdChanging(global::System.Guid value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnBasketIdChanged();
        /// <summary>
        /// There are no comments for property BillingCurrency in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string BillingCurrency
        {
          get
          {
            return this._BillingCurrency;
          }
          set
          {
            this.OnBillingCurrencyChanging(value);
            this.ReportPropertyChanging("BillingCurrency");
            this._BillingCurrency = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("BillingCurrency");
            this.OnBillingCurrencyChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _BillingCurrency;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnBillingCurrencyChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnBillingCurrencyChanged();
        /// <summary>
        /// There are no comments for property Status in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string Status
        {
          get
          {
            return this._Status;
          }
          set
          {
            this.OnStatusChanging(value);
            this.ReportPropertyChanging("Status");
            this._Status = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("Status");
            this.OnStatusChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _Status;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnStatusChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnStatusChanged();
        /// <summary>
        /// There are no comments for property SoldToName in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public string SoldToName
        {
          get
          {
            return this._SoldToName;
          }
          set
          {
            this.OnSoldToNameChanging(value);
            this.ReportPropertyChanging("SoldToName");
            this._SoldToName = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("SoldToName");
            this.OnSoldToNameChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private string _SoldToName;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSoldToNameChanging(string value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnSoldToNameChanged();
        /// <summary>
        /// There are no comments for property MarshalledData in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        public byte[] MarshalledData
        {
          get
          {
            return global::System.Data.Objects.DataClasses.StructuralObject.GetValidValue(this._MarshalledData);
          }
          set
          {
            this.OnMarshalledDataChanging(value);
            this.ReportPropertyChanging("MarshalledData");
            this._MarshalledData = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
            this.ReportPropertyChanged("MarshalledData");
            this.OnMarshalledDataChanged();
          }
        }
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        private byte[] _MarshalledData;
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnMarshalledDataChanging(byte[] value);
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        partial void OnMarshalledDataChanged();
        /// <summary>
        /// There are no comments for OrderAddresses in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("Buyseasons_transactionsModel", "FK_OrderAddresses_PurchaseOrders", "OrderAddresses")]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        [global::System.Xml.Serialization.XmlIgnoreAttribute()]
        [global::System.Xml.Serialization.SoapIgnoreAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public global::System.Data.Objects.DataClasses.EntityCollection<OrderAddress> OrderAddresses
        {
          get
          {
            return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<OrderAddress>("Buyseasons_transactionsModel.FK_OrderAddresses_PurchaseOrders", "OrderAddresses");
          }
          set
          {
            if ((value != null))
            {
              ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<OrderAddress>("Buyseasons_transactionsModel.FK_OrderAddresses_PurchaseOrders", "OrderAddresses", value);
            }
          }
        }
        /// <summary>
        /// There are no comments for OrderForms in the schema.
        /// </summary>
        [global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("Buyseasons_transactionsModel", "FK_OrderForms_PurchaseOrders", "OrderForms")]
        [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
        [global::System.Xml.Serialization.XmlIgnoreAttribute()]
        [global::System.Xml.Serialization.SoapIgnoreAttribute()]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public global::System.Data.Objects.DataClasses.EntityCollection<OrderForm> OrderForms
        {
          get
          {
            return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<OrderForm>("Buyseasons_transactionsModel.FK_OrderForms_PurchaseOrders", "OrderForms");
          }
          set
          {
            if ((value != null))
            {
              ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<OrderForm>("Buyseasons_transactionsModel.FK_OrderForms_PurchaseOrders", "OrderForms", value);
            }
          }
        }
      }
    


     


    Kevin Burton
    • Edited by KevinBurton Monday, August 8, 2011 9:27 PM Update
    Monday, August 8, 2011 9:20 PM
  • As a followup the IsLoaded/Load works. Thank you for this tip. 

    I am still looking for some advise on how best to modify the code that the designer generates for me.

    Also I notice that there is one collection in particuliar (actually the object that the collection contains) that the PK is a member just fine but there is a FK and that column is not even present in the model that the designer generates. Any ideas as to why the designer left this column out?

    Thanks again.


    Kevin Burton
    Tuesday, August 9, 2011 2:52 PM
  • I think if you switch to EF 4.1 code first you can get access the foreign key column. You have to write your own mapping and code classes using fluent UI, though.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, August 9, 2011 3:23 PM
  • I downloaded 4.1 Update 1 and not knowing how to simply regenerate the model from the database, I just removed everything and started over. Two things, one in the model I still get lines like:

            [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator""4.0.0.0")]
    

    It seems like 4.1 is not taking. Second I notice that the check box for 'Include Foreign Key...' is greyed out and disabled I cannot select it so this could be one reason that the FK column is not showing up.

     


    Kevin Burton
    Tuesday, August 9, 2011 4:16 PM
  • EF4.1 is about code first, that is, you generate database from code. check the tutorial at http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx for a glance. 

    The code for setting up mapping for foreign key columns can be found at Invert columns in FK relationship



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, August 9, 2011 8:03 PM
  • Since the database is already constructed. It would be impractical to rebuild it. I am assuming that I can use EF 4.1 with an existing database? In order to use a foreign key do I have to construct the database with EF? Why is the check box to include FK when building from the database disabled? Why is the model that is generated littered with evidence of version 4.0.0.0 rather than 4.1.0.0?

    Thank you.


    Kevin Burton
    Tuesday, August 9, 2011 10:27 PM
  • Yes you probably can write a code first DbContext that has the exact schema with your existing DB. I think the entity designer generates an ObjectContext only so you may need to write a lot of code. I might be wrong, though, since I don't use the designer I don't have a lot of experience with it, you can try ask in the entity framework forum.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, August 9, 2011 11:17 PM
  • Hi Kevin,

    >>I am assuming that I can use EF 4.1 with an existing database?

    The Answer is Yes, you just need to point Code First to an existing database is to add a App/Web.config connection string, please refer this link.

    >> Why is the check box to include FK when building from the database disabled? 

    Please try to change your project Target Framework to .Net 4.0

    In order to load the related collections(Navigation), you can use .Include()--(the same with EF4.1), in EF4.1 you can refer here

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by KevinBurton Thursday, August 25, 2011 1:02 PM
    Thursday, August 25, 2011 10:05 AM
    Moderator