why bin needs an update online after change in class model [DisplayName("Name")] - bin

I addded Data annotations in my class model, but it was not showing online, only offline.
[DisplayName("Last Name")]
public string Name { get; set; }
After some research I realised I was not doing anything wrong.
So, I decided to upload the whole site, instead of only the class file.
And it worked out.
Than I found that I needed to upload the files in the bin directory. This was sufficient after a change to the dara annotations in a class model.
So my problem is already solved. I was not syntax related, nor was the class incorrect.
Why is that? Why do I need to update the bin directory?

Related

Dynamically change models and controllers after publishing website in ASP.NET Core MVC

I'm using ASP.NET Core MVC 2. I need to operator can change some elements of Models or view codes. How I can code or design for it.
For example: I have a "news" model and I want to operator (final user of website, who can't code or access to visual studio) can add this to "news" model:
public string ImageUrl { get; set; }
and also can change the database without coding.
Thanks
If you want to design a completely extensible model, you could use something called Entity–attribute–value model (EAV).
Your model might have a couple common attributes like Title and Summary. Then you might have a list of Custom Fields, the first of which could be ImageUrl. You could create your own class called CustomField or something similar, which would have properties such as FieldName, and DataType.
public string Title { get; set; }
public string Summary { get; set; }
public List<CustomField> CustomFields { get; set; }
You would then have a table full of custom field values and the tables they belong to. It gets pretty complex.
When you want to automatically reflect your model changes to the database, you will need an ORM framework like EF (Entity Framework). You can check more here.
In order for your case to happen is to build your own configuration platform that may use several tools and mechanincs that will allow you to generate code and then compile it. Such as T4 and more.
In general, this is a very hard task to accomplish and even big experienced teams would have troubles to build something similar.
I can not post any code, as this would only seem a desperate approach.

How to have edit work like adding within Entity Framework

I have a create method that works fine. Within this create, an image is saved to a separate table. Within my Person class I have a collection of files
public virtual ICollection<File> Files { get; set; }
When I use the create method, I have this line that works fine.
db.Persons.Add(person);
This will create the person record and add the file as well. I can then view the file later.
If however, I want to change the file and I use edit, I can update all other information except for the image file. This is what I'm using to update within the edit.
db.Entry(person).State = EntityState.Modified;
Why is this? I can recreate the record but I have other relations hanging off the person table and it seems more difficult to add these back. Am I missing something fundamental, have I provided enough info?
Thanks all

Step by step process of using resource.resx files(created outside the project using class library) in asp.net mvc4

I am creating an application using mvc4. In my application i created the resource.resx file outside the project (but in one solution where my mvc application is also reside) in class library. I want to use the label name or validation message from resource.resx file.
Like:-
public class xyz
{
[Display(Name = "LBL_name", ResourceType = typeof(Resource.abcd.Resource1))]
public string Name1 { get; set; }
}
"LBL_name" is specified in Resource1.resx file. "Resource.abcd" is its custom tool namespace.
I added the reference of class library to my mvc application and set the properties of resource.resx as suggested in this link.
But it gives the following error
Cannot retrieve property 'Name' because localization failed.
Type 'Resource.abcd.Resource1' is not public or does not contain a
public static string property with the name 'LBL_name'.
I am not sure of using the resource.resx file correctly. Can someone guide me one this.
Open the file in the resource editor in VS and make sure the access modifier param is set to Public.

MVC4 Scaffolding Add Controller gives error "Unable to retrieve metadata..."

I'm using RTM version of Windows 8 and VS 2012 Ultimate. I have a MVC4 project using SqlCe 4.0 with a code first entity framework model.
Model is very simple:
public class MyThing
{
public int MyThingId { get; set; }
public int UserId { get; set; }
public string Title { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
}
When I try to create a new controller with the built in scaffolding too I get the following error:
"Unable to retrieve metadata for MyThing"
"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.
How do I get scaffolding to work?
By trial and error I found the line of code (it's the DbContext ctor) that is causing the error:
public class MyThingDb : DbContext
{
// If I comment this constructor out the scaffolding works
public MyThingDb()
: base("DefaultConnection")
{
}
public DbSet<MyThing> Things{ get; set; }
}
WTF?
I also stumbled into this symptom while running a tutorial on the subject of building an MVC Music Store application.
There definitely seem to be a bug within Visual Studio. What seems to trigger this bug is choosing some name, other than the default, used for the connection string.
My thanks goes to user dwaynef on http://forums.asp.net/t/1838396.aspx/1 for finding this workaround.
A bit elaborated you need to, temporarily during addition of the new scaffolding controller, change the name of your connection string to 'DefaultConnection' in web.config:
<connectionStrings>
<add name="DefaultConnection" ... />
</connectionStrings>
If you have more than one connection string - make sure only this one is there while performing the action.
Here's my two cents worth. I don't believe your solution actually addresses the real issue. The real fix is to pass the base constructor the database name rather than the connection string name so if your connection string is
<add name="MyContext" connectionString="Data Source=|DataDirectory|MyDatabase.sdf" providerName="System.Data.SqlServerCe.4.0" />
you're context class should be defined as
public class MyContext : DbContext
{
public MyContext() : base("MyDatabase") { }...
Hope this works for you and others as it does for me.
Here is what worked for me:
Go to connection string in web.config change the following:
providerName="System.Data.SqlClient"
instead of
providerName="System.Data.SqlServerCe.4.0"
Generate your controller.
Rename providerName back to "System.Data.SqlServerCe.4.0".
run your project.
Works for me:
In the "Add New Scaffolded Item" dialog, i added a new context (plus) with any name (for me "ScaffoldingContext").
Then the scaffolding works. Just rename the context in the Controller.
After trying different options, the below method resolves the error..
If the name value in connection string matches with the value passed to the constructor, it works.
public MyThingDb()
: base("name=MyContext")
{
}
Problem may be because of missing
[NotMapped] Attribute
in one of the model class.
As I missed the attribute and I was cunning my head.
[Display(Name="Logo")]
[DataType(DataType.Upload)]
[NotMapped]
public HttpPostedFileBase Logo { set; get; }
This may sometimes be caused by an association property or a foreign key attribute
I had a similar issue but it wasn't the default constructor. It also happens if you have multiple projects in your solution and your "Web" facing MVC project does not reference EntityFramework.
Change "Things" inside
public
DbSet<MyThing> Things
{ get; set; }
}
to
"Database1" where "Database1" is the name of the database file on disk which appears in your Web.config file as "Database1.sdf"
The solution that worked for me is to pass the same database name as in your connection string to the base constactor of your dbContext class.
I solved this by pressing CTRL+F5 to rebuild my project before adding my controller.
I had a similar issue when trying to create a view from my controller using the scaffolding. In the create view dialog I simply cleared the "Data Context Class" dropdown and then the scaffolding mechanism worded fine.
This is what that worked out for me ..
I commented out the connectionstring using the 'System.Data.SqlServerCe.4.0' and
then added the controller with scaffolding templates.
In your context class you have to comment the DbConfigurationType when you are going to create a controller with scaffolding.
//[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class NameDbContext
{}
MVC 5 / EF 6
perhaps you can do this in the older version?
commented out the connection strings in the web / app.config then save
try to create new controller and have VS create a "new" dbcontext item instead of choosing the one you already have
click create
delete new dbcontext class
replace controller dbcontext with yours
uncomment connection strings in web / app.config then save
worked for me!
I had the same error message, but it was nothing to do with the connection string.
It is a very rare case, but hopefully this will help someone. My model name was the same as one of the segments of my namespace name.
For example:
namespace blah.blah.Building
public class Building
I renamed my namespace and fixed all usages and then the t4 scaffolding worked!
Her's another possible solution. You may have to run scaffolding for "dependent" models first. Then work your way up to complicated models that have many dependencies.

MVC 3 - Entity Framework - Scaffolding - Validation issue

Im developing an MVC 3 application with Entity Framework and Im tring to use Scaffolding.
To solve "Type not mappedd issue" I've done the procedure found here. Everything now works fine.
Default validation is not working, required field are firing an exception instead of write something on ValidationSummary, so I want to add my custom validations using attributes.
The problem is that the solution about "type not mapped issue" has added 2 .tt files and a .cs file for each of my entities, these files are recreated each time my model (.edmx) is changed and saved so I cant put my Data Annotation Validator Attributes in those classes and either I cant create a new partial class with some properties because thay are already defined.
How can I do? May I have to move validation client-side using jquery? Or maybe there a workaround to add Data Annotation Validator Attributes to my entities, I prefer this way to have more visibility of my validations.
Thanks in advance
I've not used the DbContext generator, but have had similar issues with the POCO Generator. Assuming that the solution is similar:
Modify the T4 template that creates the entity classes to add an extra attribute to the class:
[MetadataType(typeof(CustomerMetaData))]
where "Customer" is the name of the entity.
Then, manually create MetaData classes for each of your entities. You can actually use a T4 template for that, too, if you want, but not have it run all the time.
The Metadata classes look like this...
public class CustomerMetaData
{
[StringLength(150, ErrorMessage="Maximum length is 150 characters.")]
[Required(ErrorMessage="CustomerName is required.")]
public virtual string CustomerName
{
get;
set;
}
public virtual Nullable<int> Type
{
get;
set;
}
// ... etc ...
}
As you can see, you attach the rules to the MetaData class, thus abstracting it from the generated entity class.

Resources