In my entity class i use enum as a key property:
[DataContract]
public class MultimediaType
{
[DataMember]
[Key]
public Identificator Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataContract]
public enum Identificator
{
[EnumMember]
Image = 1,
[EnumMember]
Video = 2,
[EnumMember]
Sound = 3
}
}
[EnableClientAccess()]
public class DomService : DomainService
{
List<MultimediaType> _multimediaTypes = new List<MultimediaType>()
{
new MultimediaType()
{
Id = MultimediaType.Identificator.Image,
Name = "Image",
Description = "This is type for all images."
},
new MultimediaType()
{
Id = MultimediaType.Identificator.Video,
Name = "Video",
Description = "This is type for all videos."
},
new MultimediaType()
{
Id = MultimediaType.Identificator.Sound,
Name = "Sound",
Description = "This is type for all sounds."
},
};
[Query]
public IQueryable<MultimediaType> GetMultimediaTypes()
{
return _multimediaTypes.AsQueryable();
}
}
My client is SilverLight application that consume my domain service.
I have no idea what is wrong, but i obtaint this error:
The property 'MultimediaType.Id' is marked as a key property, but it's not serializable. Are you missing DataMemberAttribute?
Do you have any idea what i do wrong? Thanks a lot!
EnumMemberAttribute : Specifies that the field is an enumeration member and should be serialized.
Check the below lins :
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.enummemberattribute(v=vs.95).aspx
Sharing Enum with WCF Service
Also take a look at the Remarks and Example
Related
I created
public class Video
{
public int Id { get; set; }
[MaxLength(50)]
public string Nom { get; set;}
public ICollection<Langue> Langue { get; set; }
}
public class Langue
{
public int LangueId { get; set; }
[Required]
public string Designation { get; set; }
public ICollection<Video> Video { get; set; }
}
Migration is ok and 3 tables are created, Video, Langue and
VideoLangue with 2 fields VideoId and LangueId
It’s a many to many relationship.
I want to entrer data in OnModelCreating
public class VideoDbContext : DbContext
{
public VideoDbContext(DbContextOptions<VideoDbContext> option) : base(option)
{
}
public DbSet<Video> Video { get; set; }
public DbSet<Langue> Langue { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var langFR = new Langue() { LangueId = 1, Designation = "Français" };
var langIT = new Langue() { LangueId = 2, Designation = "Italien" };
var langCH = new Langue() { LangueId = 3, Designation = "Chinois" };
var langAN = new Langue() { LangueId = 4, Designation = "Anglais" };
modelBuilder.Entity<Video>().HasData(
new Video() { Id = 1, Nom = "007", Langue = new List<Langue> { langFR,langCH,langIT } }
);
When i do the migration i have this error
The seed entity for entity type 'Video' cannot be added because it has the navigation 'Langue' set. To seed relationships, add the entity seed to 'VideoLangue (Dictionary<string, object>)' and specify the foreign key values {'VideoId'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the involved property values.
According to this error i use an other way
i create a VideoLangue Class
public class VideoLangue
{
public int VideoId { get; set; }
public int LangueId { get; set; }
}
I modify the VideoContext
public class VideoDbContext : DbContext
{
public VideoDbContext(DbContextOptions<VideoDbContext> option) : base(option)
{
}
public DbSet<Video> Video { get; set; }
public DbSet<Langue> Langue { get; set; }
public DbSet<VideoLangue> VideoLangue { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Langue>().HasData(
//remplissage langue
new Langue() { LangueId = 1, Designation = "Français" },
new Langue() { LangueId = 2, Designation = "Italien" },
new Langue() { LangueId = 3, Designation = "Chinois" },
new Langue() { LangueId = 4, Designation = "Anglais" }
);
modelBuilder.Entity<Video>().HasData(
new Video() { Id = 1, Nom = "007}
);
modelBuilder.Entity<VideoLangue>()
.HasKey(o => new { o.VideoId, o.LangueId });
modelBuilder.Entity<VideoLangue>().HasData(
////remplissage VideoLangue
new VideoLangue() { VideoId = 1, LangueId = 1 },
new VideoLangue() { VideoId = 1, LangueId = 3 },
new VideoLangue() { VideoId = 1, LangueId = 4 }
);
When i do the migration i have
Cannot use table 'VideoLangue' for entity type 'VideoLangue (Dictionary<string, object>)' since it is being used for entity type 'VideoLangue' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'VideoLangue (Dictionary<string, object>)' on the primary key properties and pointing to the primary key on another entity typed mapped to 'VideoLangue'.
How can we put data on langue via OnModelreating
Do you have an idea ?
Thank you
So I am trying to create my first block. The idea of this block is to get latest news from an api end point and then show it on different pages on the website.
What I have understood is this
Create a block type, something like this
public class NewsBlock : BlockData
{
[CultureSpecific]
[Display(
Name = "Heading",
Description = "Add a heading.",
GroupName = SystemTabNames.Content,
Order = 1)]
public virtual String Heading { get; set; }
}
Then I create a model for my Block
public class LatestNewsViewModel
{
public NewsBlock NewsBlock { get; private set; }
public IEnumerable<dynamic> LatestNews { get; set; }
public LatestNewsViewModel(NewsBlock latestNewsBlock, IEnumerable<dynamic> latestNews)
{
NewsBlock = latestNewsBlock;
LatestNews = latestNews;
}
}
Then I creata a block controller and in the index action I get data from my api and fill the block container data
Then I create a partial view and then from controller pass data into the view
Then from the dashboard I can add my block where ever I want on the site
Is this the way to do it? Or am I missing something?
That seem about correct. Please note there are many ways and opinions on how to get your data from the content model through the controller to the actual view. The example below is just the most simple scenario I can come up with.
public class NewsBlock : BlockData
{
[CultureSpecific]
[Display(
Name = "Heading",
Description = "Add a heading.",
GroupName = SystemTabNames.Content,
Order = 1)]
public virtual String Heading { get; set; }
}
The controller
public class NewsBlockController : BlockController<NewsBlock>
{
// GET: NewsBlock
public override ActionResult Index(NewsBlock currentBlock)
{
// apistuff
ApiModelWhatever returnFromApi = "whatever";
var model = new LatestNewsViewModel(currentBlock, returnFromApi);
return PartialView(model);
}
}
ViewModel
public class LatestNewsViewModel
{
public string Heading { get; private set; }
public ApiModelWhatever ReturnFromApi { get; private set; }
public LatestNewsViewModel(NewsBlock latestNewsBlock, ApiModelWhatever returnFromApi)
{
Heading = latestNewsBlock.Heading;
ReturnFromApi = returnFromApi;
}
}
View
#model LatestNewsViewModel
<h2>#Html.PropertyFor(model => model.Heading)</h2>
This is database first using entity frameworks 6 and lazy loading.
I have the following class of device that is missing four foreign key ids:
public partial class Device {
public int SolutionId { get; set; }
public string SiteId { get; set; }
public string Name { get; set; }
public int SysId { get; set; }
public Nullable<int> SysType { get; set; }
public string SerialNumber { get; set; }
public Nullable<int> ParentId { get; set; }
public virtual DeviceModel DeviceModel { get; set; }
public virtual DeviceType DeviceType { get; set; }
public virtual SolutionApplication SolutionApplication { get; set; }
public virtual SolutionType SolutionType { get; set; }
}
Missing Keys/Ids that are not generated but automatically got mapped as virtual objects:
DeviceModelId, DeviceTypeId, SolutionApplicationId, and SolutionTypeId
I want to save a new device using breeze but it is looking for the device type. I tried adding the deviceTypeId before saving:
newDevice.deviceTypeId = 5;
But it still doesn't get read and does not get saved.
[Error] Save failed: Entities in 'PortalEntities.Devices' participate in the 'FK_Devices_DeviceTypes' relationship. 0 related 'DeviceType' were found. 1 'DeviceType' is expected.
This is how my save statement in my breeze controller:
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _repository.SaveChanges(saveBundle);
}
I checked what actually is getting passed as it attempts to do a save. The save bundled contained the following entity. BUT the device type which is a required field is not there, hence the error that its missing.
"entities": [
{
"SolutionId": -1,
"SiteId": "11111d2",
"Name": "asdf",
"SysId": 0,
"SysType": null,
"SerialNumber": null,
"ParentId": null,
"entityAspect": {
"entityTypeName": "Device:#HtPortal.Data.Portal",
"defaultResourceName": "Devices",
"entityState": "Added",
"originalValuesMap": {},
"autoGeneratedKey": {
"propertyName": "SolutionId",
"autoGeneratedKeyType": "Identity"
}
}
}
Since the deviceTypeId did not work, so I tried adding the deviceType right before saving:
newDevice.deviceType = { id:5, name: 'Blue'}; //NOTE: This already exists in the devicetype table in the database
But I get the following error:
TypeError: Cannot read property 'entityState' of undefined
Using Breeze, How do I add this foreign entity so that I can save this new device?
Edit 1: This is my DeviceType Class:
public partial class DeviceType {
public DeviceType() {
this.Devices = new HashSet<Device>();
}
public byte Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
Just add the existing DeviceType object from your dbContext.
Something like:
using (YourDbEntities context = new YourDbEntities())
{
Device newDevice = new Device();
//whatever initialisation you need do....
newDevice.DeviceType = context.DeviceTypes.Find(5)
// the above line assumes that id is PK for DeviceType and you want the entry
// with id==5 - if not, other ways such as DeviceTypes.Where(d=>d.Id==5).First()
// to find the object in your existing db will work too!
context.Devices.Add(newDevice);
context.SaveChanges();
}
I'm just learning ASP.NET MVC 3, And recently I tried a lot of times to pass arrays/lists/ICollections etc. but couldn't. everytime the list was empty.
For example, the current project:
Model:
public class Video
{
public int VideoID { get; set; }
public string Name { get; set; }
public ICollection<string> Tags { get; set; }
}
Initializer - Seed:
protected override void Seed(DatabaseContext context)
{
var videos = new List<Video>
{
new Video {
Name = "Video01",
Tags = new List<string> { "tag1", "tag2" },
};
videos.ForEach(s => context.Videos.Add(s));
context.SaveChanges();
base.Seed(context);
}
In the view: I do get the Name property, but the Tags are completely empty.
In the debug I get Tags - Count: 0.
This is not the first time it happens to me, to be honest it happens every single time when I try to pass those kind of stuff. a bit of info about the project:
ASP.NET MVC 3, Entity-Framework:Code First, SqlServerCe.4.0.
Crean an entity Tag
public class Video
{
public int VideoID { get; set; }
public string Name { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public int VideoId { get; set; }
public string TagText { get; set; }
}
or store tags to one field separated with comma /semicolon or whatever fits for your solution
By default Entity Framework doesn't load associations of an entity, you need to specify it explicitly:
var videos = context.Videos.Include("Tags");
Hi I have class to sen via ria service.
class look like
[DataContract]
public partial class AttributeNode
{
[DataMember]
[Key]
public int Uid { get; set; }
public AttributeNode()
{
this.Children = new List<String>();
}
private String text;
[DataMember]
public String Text
{
get
{
return text;
}
set
{
text = value;
this.Uid = text.GetHashCode();
}
}
[DataMember]
[Include]
[Association("AttributeNode_AttributeNode", "Uid", "Uid")]
public List<AttributeNode> Children { get; set; }
public void AddChild(AttributeNode child)
{
this.Children.Add(child);
}
}
The problem is that when I recive object to client it's not ok. It always as a Children contain itself. Problem is on List of same type. Help?
Tnx!!
I suppose this is some kind of parent-child tree structure.
The Association tag is used to say "this key" and "other key".
Your AttributeNode class needs a Id property to tell which its parent.
You'd need
[Key]
public int Uid { get; set; }
public int ParentUid { get; set; }
[Include]
[Association("AttributeNode_AttributeNode", "Uid", "ParentUid")]
public List<AttributeNode> Children { get; set; }