I have an issue with the RazorRockstars example. I have renamed the main route (/rockstars) on the Rockstars request class to /properties and now it no longer loads. It appears /properties route is reserved. Is this the case? I wish to use this route in my application.
Works:
[Route("/rockstars")]
[Route("/rockstars/{Id}")]
[Route("/rockstars/aged/{Age}")]
public class Rockstars
{
public int? Age { get; set; }
public int Id { get; set; }
}
Works:
[Route("/blahblahblah")]
[Route("/blahblahblah/{Id}")]
[Route("/blahblahblah/aged/{Age}")]
public class Rockstars
{
public int? Age { get; set; }
public int Id { get; set; }
}
Does not work:
[Route("/properties")]
[Route("/properties/{Id}")]
[Route("/properties/aged/{Age}")]
public class Rockstars
{
public int? Age { get; set; }
public int Id { get; set; }
}
Using /properties doesn't work in development because it matches a folder in your root directory (which hi-jacks the request), i.e. in this case VS.NET's Properties/ folder it uses to hold your projects AssemblyInfo.cs file.
It will work after you rename the Properties folder to something else, or once you deploy since deployment builds doesn't include the Properties folder.
Related
I'm using E.F 6.2 code first in my MVC project and configure the project for using Asp.Net Identity 2 for accounting. I have a "BaseData" entity, that contains all of my basic data. Here is the class body:
Parent Entity:
public class BaseData
{
public int Id { get; set; }
public string Caption { get; set; }
public virtual ICollection<A> Children1 { get; set; }
public virtual ICollection<B> Children2 { get; set; }
.
.
.
public virtual ICollection<ZZZ> ChildrenN { get; set; }
}
So the other entities must be related to it. In my case, each child entity has hundreds of relations to the "BaseData" parent entity and I have hundreds of child entities. So there is thousands of relations to the parent entity. Here is the pseudo code for child entities:
Child Entities:
public class A
{
public int Id { get; set; }
public int BaseData1Id { get; set; }
public BaseData BaseData1 { get; set; }
public int BaseData2Id { get; set; }
public BaseData BaseData2 { get; set; }
.
.
.
public int BaseData300Id { get; set; }
public BaseData BaseData300 { get; set; }
}
.
.
.
.
public class ZZZ
{
public int Id { get; set; }
public int BaseData1Id { get; set; }
public BaseData BaseData1 { get; set; }
public int BaseData2Id { get; set; }
public BaseData BaseData2 { get; set; }
.
.
.
public int BaseData300Id { get; set; }
public BaseData BaseData300 { get; set; }
}
I'm using E.F fluent API to configure the relationship between my entities. The problem is that when I run the project and want to fetch some data like user account, I get the "StackOverFlow" exception !!
For solving the problem, I try to remove child entities one by one, and re-creating the database. I found that when the children decreases, the problem is solved and I didn't get exception but I don't know really why !!
Is there limitation for navigation properties on E.F ?
Is there a solution to increase the stack capacity for preventing "StackOverFlow" exception ?
I am trying to implement DbContext for couple of tables called 'Employee' and 'Department'
Relationship between Employee and Department is many to one. i.e. department can have many employees.
Below are the EntityFramework classes I designed ( CodeFirst approach )
[Table("Employee")]
public class Employee
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Department_ID")]
public int Department_ID { get; set; }
public virtual Department Department { get; set; }
}
[Table("Department")]
public class Department
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Column("Name")]
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
While adding Employee record I am getting below exception
"Invalid column name 'Department_ID1'."
I am not sure why EF is referring to Department_ID1. Do I need to add configuration in OnModelCreating method of DbContext?
I am using EF version 6.1.1
I've also gotten this problem in my EF one-many deals where the one has a List of the many property and my mapping didn't specify that property. For example take:
public class Notification
{
public long ID { get; set; }
public IList<NotificationRecipient> Recipients { get; set; }
}
then
public class NotificationRecipient
{
public long ID { get; set; }
public long NotificationID { get; set; }
public Notification Notification { get; set; }
}
Then in my mapping, the way that caused the Exception (the incorrect way):
builder.HasOne(x => x.Notification).WithMany()
.HasForeignKey(x => x.NotificationID);
What fixed it (the correct way) was specifying the WithMany property:
builder.HasOne(x => x.Notification).WithMany(x => x.Recipients)
.HasForeignKey(x => x.NotificationID);
Hi After spending some time I could fix this problem by using ForeignKey attribute on public virtual Department Department { get; set; } property of Employee class.
Please see below code.
[Table("Employee")]
public class Employee
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Department_ID")]
public int Department_ID { get; set; }
[ForeignKey("Department_ID")]
public virtual Department Department { get; set; }
}
This fixed my problem. Are there any other solution to fix this? Using fluent API?
For me, the issue was resolved by removing a (duplicate?) virtual property.
Using the OP's example:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Department_ID { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
Turns into:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Department_ID { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
In my case I added a virtual property on top of the auto generated property
I fixed it by adding the NotMapped attribute to my property, or you could configure with fluent api
public partial class Control
{
[NotMapped]
public virtual ICollection<Control> Children { get => this.InverseParent; set => this.InverseParent = value; }
}
I had the same error, my issue was the FK was a long but I had it as an int in the model. EF generated a new column because it didn't match types on the FK so it assumed they weren't the same and went ahead with making another one but putting 1 at the end because there was already one with the proper name. Making sure the types matched resolved the issue for me.
This can be fixed simply by putting [NotMapped] annotation on your virtual properties.
public class Employee
{
[ForeignKey("Department")]
public int Department_ID
[NotMapped]
public virtual Department Department { get; set; }
}
And in you modelBuilder:
modelBuilder.Entity<Employee>(entity =>
{
entity.HasOne(e => e.Department);
});
Just flip this around if you want to call by Department.
We use the [NotMapped] annotation so that EF Core will disregard it when looking at your database.
I'm looking to create some custom API apps for the sole purpose of creating a/some Logic Apps. With these custom API Apps, I want to pass around files. These files will usually be CSV, ZIP, Excel, TXT, and some other formats - unknown to the consumer until the file is returned (i.e. the client does not dictate the file format).
How does one do something like this in a way that's compatible with Swagger/Swashbuckle, Web API, and Logic Apps? I'll ultimately be tying this into an FTP connector, Dropbox, Onebox, or other file-storage connector.
Does following something like this work or do I need to take a different approach? For example, should I simply work with JSON objects and let my binary be base64-encoded by using a model like this?
public class BinaryFile
{
public string FileName { get; set; }
public string FileExtension { get; set; }
public string DeducedMimeType { get; set; }
public int FileSize { get; set; }
public string FileEncoding { get; set; }
public byte[] FileBinary { get; set; }
}
(this question is cross-posted to MSDN Forums)
The approach I've taken is what I posed in the question with the BinaryFile class. I've broken it out into a few classes. I'm not done - I have some improvements to make still but this is functional right now.
Main class with some common fields:
public class FileResult<T>
{
public string Source { get; set; }
public T File { get; set; }
public IList<string> ErrorMessages { get; set; } = new List<string>();
public bool IsSuccessful { get; set; } = false;
}
This is the <T> in my FileResult<T> class:
public class CsvFile
{
public string Filename { get; set; }
public string Contents { get; set; }
public int Size { get; set; }
}
public class BinaryFile
{
public string FileName { get; set; }
public string FileExtension { get; set; }
public string DeducedMimeType { get; set; }
public int Size { get; set; }
public byte[] Contents { get; set; }
}
Note that in my case, there are some times when I am working with multiple files and not just one, so what could appear to be some common fields are still within the type passed in as the <T> so I can have something like FileResult<IEnumerable<CsvFile>>.
This is all playing nicely with Swagger, Swashbuckle, Web API, Azure API Apps, and Azure Logic Apps. For the cases where I am returning multiple files to my Azure Logic App, I use the fairly hidden splitsOn feature (that also has some designer bugs, so be careful!) to easily iterate over all of the files. It works very nicely.
Tried to write service to work with following RequestDTO
[Route("/templates", "POST", Summary = "Creates new template")]
public class CreateTemplate : IReturn<ExecutionResult>
{
public Guid TemplateId { get; set; }
public string Name { get; set; }
public string DefaultContent { get; set; }
}
But ServiceStack returns "Static File '/templates/' not found." As far as I can see /Templates folder can be used to define templates for /metadata. But I do not have such folder at all.
Lets say I have an object called Company that is represented like so:
public class Company
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public virtual int Id { get; set; }
public virtual string Line1 { get; set; }
public virtual string Line2 { get; set; }
public virtual string Line3 { get; set; }
public virtual string Town { get; set; }
public virtual string County { get; set; }
public virtual string Postcode { get; set; }
public virtual double Longitude { get; set; }
public virtual double Latitude { get; set; }
}
When I create a new company, I also want the new address to be created too. At the moment, I can only get this to work by creating the address record and then creating the company record separately:
[HttpPost]
public ActionResult Add(Company company)
{
_addressRepo.Create(company.Address);
_companyRepo.Create(company);
return RedirectToAction("Index");
}
(Where _addressRepo and _companyRepo are of type IRepository<Address> and IRepository<Company> respectively)
How can I get all child objects to cascade save? So that ideally, when I call _companyRepo.Create(company);, Orchard handles all the child records for me?
I tried creating a new attribute so that properties can be marked as Cascade Save Update, but If I put a breakpoint on the Apply method it never appears to get hit.
public class CascadeSaveUpdateAttribute : Attribute {
}
public class CascadeSaveUpdateConvention :
AttributeCollectionConvention<CascadeSaveUpdateAttribute>
{
protected override void Apply(CascadeSaveUpdateAttribute attribute, ICollectionInstance instance)
{
instance.Cascade.SaveUpdate();
}
}
What can I do to get this Cascade Save functionality working?