Hello
I am new to WCF DataService. I want to expose DataContext Property at Entity level . But when I try to update service at view side it is giving following error:
The server encountered an error processing the request. The exception message is 'The property 'Regions' on a complex type 'WcfService1.DataClasses1DataContext' is not a valid property.
Navigation properties are not supported on complex types.'."
Following is my code for Entity.
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WcfService1
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="Northwind")]
public partial class DataClasses1DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertRegion(Region instance);
partial void UpdateRegion(Region instance);
partial void DeleteRegion(Region instance);
partial void InsertTerritory(Territory instance);
partial void UpdateTerritory(Territory instance);
partial void DeleteTerritory(Territory instance);
#endregion
public DataClasses1DataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Region> Regions
{
get
{
return this.GetTable<Region>();
}
}
public System.Data.Linq.Table<Territory> Territories
{
get
{
return this.GetTable<Territory>();
}
}
}
// [System.Data.Services.IgnoreProperties("DataContext")]
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Region")]
public partial class Region : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _RegionID;
private string _RegionDescription;
private EntitySet<Territory> _Territories;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnRegionIDChanging(int value);
partial void OnRegionIDChanged();
partial void OnRegionDescriptionChanging(string value);
partial void OnRegionDescriptionChanged();
#endregion
public Region()
{
this._Territories = new EntitySet<Territory>(new Action<Territory>(this.attach_Territories), new Action<Territory>(this.detach_Territories));
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RegionID", DbType="Int NOT NULL", IsPrimaryKey=true)]
public int RegionID
{
get
{
return this._RegionID;
}
set
{
if ((this._RegionID != value))
{
this.OnRegionIDChanging(value);
this.SendPropertyChanging();
this._RegionID = value;
this.SendPropertyChanged("RegionID");
this.OnRegionIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RegionDescription", DbType="NChar(50) NOT NULL", CanBeNull=false)]
public string RegionDescription
{
get
{
return this._RegionDescription;
}
set
{
if ((this._RegionDescription != value))
{
this.OnRegionDescriptionChanging(value);
this.SendPropertyChanging();
this._RegionDescription = value;
this.SendPropertyChanged("RegionDescription");
this.OnRegionDescriptionChanged();
}
}
} private DataClasses1DataContext _DataContext;
public DataClasses1DataContext Context
{
get { return _DataContext; }
set { _DataContext = value; }
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Region_Territory", Storage="_Territories", ThisKey="RegionID", OtherKey="RegionID")]
public EntitySet<Territory> Territories
{
get
{
////DataClasses1DataContext DataContext = this.GetDataContext();if (DataContext != null && _Territories == null)
{
// DataContext.LoadProperty(this, "Territories");
const BindingFlags FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var method = DataContext.GetType().GetMethod("ClearCache", FLAGS);
method.Invoke(DataContext, null);
// DataContext.DeferredLoadingEnabled = false;
var MasterUnitsloadOption = new DataLoadOptions();
MasterUnitsloadOption.LoadWith<Region>(p => p.Territories);
DataContext.LoadOptions = MasterUnitsloadOption;
}
return this._Territories;
}
set
{
this._Territories.Assign(value);
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_Territories(Territory entity)
{
this.SendPropertyChanging();
entity.Region = this;
}
private void detach_Territories(Territory entity)
{
this.SendPropertyChanging();
entity.Region = null;
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Territories")]
public partial class Territory : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private string _TerritoryID;
private string _TerritoryDescription;
private int _RegionID;
private int _Id;
private EntityRef<Region> _Region;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnTerritoryIDChanging(string value);
partial void OnTerritoryIDChanged();
partial void OnTerritoryDescriptionChanging(string value);
partial void OnTerritoryDescriptionChanged();
partial void OnRegionIDChanging(int value);
partial void OnRegionIDChanged();
partial void OnIdChanging(int value);
partial void OnIdChanged();
#endregion
public Territory()
{
this._Region = default(EntityRef<Region>);
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TerritoryID", DbType="NVarChar(20) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]
public string TerritoryID
{
get
{
return this._TerritoryID;
}
set
{
if ((this._TerritoryID != value))
{
this.OnTerritoryIDChanging(value);
this.SendPropertyChanging();
this._TerritoryID = value;
this.SendPropertyChanged("TerritoryID");
this.OnTerritoryIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TerritoryDescription", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
public string TerritoryDescription
{
get
{
return this._TerritoryDescription;
}
set
{
if ((this._TerritoryDescription != value))
{
this.OnTerritoryDescriptionChanging(value);
this.SendPropertyChanging();
this._TerritoryDescription = value;
this.SendPropertyChanged("TerritoryDescription");
this.OnTerritoryDescriptionChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_RegionID", DbType="Int NOT NULL")]
public int RegionID
{
get
{
return this._RegionID;
}
set
{
if ((this._RegionID != value))
{
if (this._Region.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnRegionIDChanging(value);
this.SendPropertyChanging();
this._RegionID = value;
this.SendPropertyChanged("RegionID");
this.OnRegionIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", AutoSync=AutoSync.Always, DbType="Int NOT NULL IDENTITY", IsDbGenerated=true)]
public int Id
{
get
{
return this._Id;
}
set
{
if ((this._Id != value))
{
this.OnIdChanging(value);
this.SendPropertyChanging();
this._Id = value;
this.SendPropertyChanged("Id");
this.OnIdChanged();
}
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Region_Territory", Storage="_Region", ThisKey="RegionID", OtherKey="RegionID", IsForeignKey=true)]
public Region Region
{
get
{
return this._Region.Entity;
}
set
{
Region previousValue = this._Region.Entity;
if (((previousValue != value)
|| (this._Region.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Region.Entity = null;
previousValue.Territories.Remove(this);
}
this._Region.Entity = value;
if ((value != null))
{
value.Territories.Add(this);
this._RegionID = value.RegionID;
}
else
{
this._RegionID = default(int);
}
this.SendPropertyChanged("Region");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
#pragma warning restore 1591
If I remove DataContext property from Region class then it will work fine.
But my issue is I want to use Territories property from Region object (SetProperty) . So I have to get dataContext at entity level to set LoadOpetions property of DataContext.
But here it is giving error. So can anybody please guide me how to use it?
Or how the structure it should be to use LinqToSql with WCF DataService with WPF?
I want to expose EntityRef & EntitySet for all entities so after searching I got that we can do that If we use loadopetion of datacontext. But here If I add this property then after rebuild service project When I try to update Reference at view side
it is giving error.
Please help me for this issue.
Regards
Vipul