Keyword not supported: 'name' - c#

So I'm going through this tutorial that seems so simple but I can't seem to get it to work.
http://msdn.microsoft.com/en-us/data/gg685489
This is the error I'm receiving when running my app: "Keyword not supported: 'name'."
Now I've looked at other posts similar to mine and it seemed like the connection string was the issue. So I looked closely at but can't see any real differences.
<add name="BBCommercialSolutionsEntities"
connectionString="metadata=res://*/Models.BBCommercialSolutions.csdl|res://*/Models.BBCommercialSolutions.ssdl|res://*/Models.BBCommercialSolutions.msl;provider=System.Data.SqlClient;provider connection string="data source=MYSOURCENAME;initial catalog=MYDATABASENAME;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
In my CompanyController.cs class, I receive the error when using the .ToList().
public ActionResult Index()
{
//return View();
using (var db = new BBCommercialSolutionsEntities())
{
//return View(db.BBCSCompanies.ToList());
var tbl = db.BBCSCompanies;
var list = tbl.ToList();
return View(tbl.ToList());
}
}
And "new BBCommercialSolutionsEntities()" goes to my auto-generated template
public BBCommercialSolutionsEntities()
: base("name=BBCommercialSolutionsEntities")
{
}
Any ideas, thoughts, explanations, rants would help.

Just use BBCommercialSolutionsEntities
public BBCommercialSolutionsEntities() : base("BBCommercialSolutionsEntities")
{
}

Related

How to fix The controller for path '/' was not found or does not implement IController

I just deployed a new controller to my production environment. For some reason, the controller does not get called. All other controllers on the site work fine. This is the only one that is failing. What I keep getting is the error:
Error rendering controller BlogListing.GetIndex: Could not create
controller: 'BlogListing'. The controller for path '/' was not found
or does not implement IController
I've spent about 3 hours trying to troubleshoot this. I have:
Added debug code into the controller to see if it is in fact being called. My debug statements does not get hit.
Verified the name of the controller is correct
I am using the default MVC routing.
Thinking that it might be a missing dependent dll, I copied all of the dlls from my production environment (where it is not working) to my local environment and it came right up
Checked file system permissions thinking that somehow it couldn't be read.
I did look at other posts regarding similar issues but none of those solutions worked or were not applicable
namespace Portal.Features.Blog.Controllers
{
using Glass.Mapper.Sc;
using Glass.Mapper.Sc.Web.Mvc;
using Sitecore.Data.Items;
using System;
using System.Linq;
using System.Web.Mvc;
using Portal.Foundation.Blog;
using portal.ct.gov.Models;
using Portal.Features.Blog.Models;
using portal.ct.gov.Business;
public class BlogListingController : GlassController
{
public ActionResult GetIndex(string keyword = "", string page = "", string author = "")
{
Sitecore.Diagnostics.Log.Info("Blog Controller found", "portal.ct.gov");
try
{
SitecoreContext scContext = new SitecoreContext();
Item contextItem = scContext.GetCurrentItem<Item>();
Item blogHome = null;
//Get Blog Root
if (contextItem != null)
{
blogHome = contextItem.Axes.SelectSingleItem("ancestor-or-self::*[##templatename = 'Blog Section']");
}
var sKeyword = !string.IsNullOrEmpty(HttpContext.Request.QueryString[Constants.QueryStrings.SearchKeyword]) ? HttpContext.Request.QueryString[Constants.QueryStrings.SearchKeyword] : string.Empty;
var blogAuthor = !string.IsNullOrEmpty(HttpContext.Request.QueryString["author"]) ? HttpContext.Request.QueryString["author"] : string.Empty;
var blogCategory = !string.IsNullOrEmpty(HttpContext.Request.QueryString["category"]) ? HttpContext.Request.QueryString["category"] : string.Empty;
var blogPage = !string.IsNullOrEmpty(HttpContext.Request.QueryString["page"]) ? HttpContext.Request.QueryString["page"] : "1";
var model = GetBlogListing(blogHome, sKeyword, blogCategory, blogAuthor, Convert.ToInt32(blogPage));
return View("/views/blog/BlogResultsMain.cshtml", model);
}
catch(Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error processing bloglisting-->getINdex " + ex.Message, ex, "portal.ct.gov");
return View("/views/blog/BlogResultsMain.cshtml");
}
}
}
Any help is appreciated. Please note that I am using Sitecore CMS.
It is worth checking the cached MVC-ControllerTypeCache.xml file in folder c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\NAMEOFYOURAPP\xxxxx\xxxxxxxx\UserCache\.
If you can't find your controller there, remove the cached xml file and restart your website. More details you can find here

ASP.NET cannot parse xml! Check file path

I created a simple API via ASP.NET MVC 4:
public class ActionController : ApiController
{
[WebMethod]
public string getCommunities()
{
try
{
MethodClass method = new MethodClass();
return method.getCommunities();
}
catch(Exception ex)
{
return ex.Message.ToString();
}
}
}
which is trying to call this method in the Method class:
public string getCommunities()
{
return "bbb";
}
but for whatever reason, I get this error:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cannot parse xml! Check file path</string>
I tried Googling for the error, but came up with nothing, has anyone seen this error before? and how do I fix it?
As already pointed in comments, you are looking for your bug in the wrong place. method.getCommunities() is throwing an error with message "cannot parse xml! Check file path".
Googling your error it seems to me that you are throwing a custom exception: searching for that string in your code may point you to the right place.
As a quick proof of concept I changed the standard API generated by Visual Studio Web API template.
public string Get(int id)
{
try
{
var t = 0;
var i = 1 / t;
return "bbb";
}
catch { return "ABBA"; }
}
which exactly returns the custom error message as xml string
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ABBA</string>
I attempted to replicate the case you mention by creating simple ActionController.cs in ASP.Net MVC 4 template as follow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Services;
namespace MvcApiApplicationTrial1.Controllers
{
public class ActionController : ApiController
{
[WebMethod]
public string getCommunities() {
try {
MethodClass method = new MethodClass();
return method.getCommunities();
} catch (Exception ex) {
return ex.Message.ToString();
}
}
}
public class MethodClass
{
public string getCommunities() {
return "bbb";
}
}
}
And call it in the web browser (Chrome) with the following url:
http://localhost:56491/api/Action/getCommunities
And get the following correct result:
If you declare, define, and call things right, your code should have no problem at all.
So, I suggest you to re-check your declaration, definition, as well as your calling to the related Controller/Method again. Your problem may lay somewhere else.
And since the error seems to be a custom error, judging from the code posted alone, likely that the problem lays somewhere in your getCommunities method. Check the method, try to find the "cannot parse xml!" text there. Alternatively, but less likely, the error is in the MethodClass constructor. Same thing, check your MethodClass, try to find the "cannot parse xml!" text.
As for the given case as what you have posted in your question, I found no issue at all.
But anything else in between try and "bbb" can also potentially be the source of the created error. Checking the error text would be my first step if there are more things in the try block and I am unsure where the error may actually be generated.
in Global.asax.cs should put code bellow:
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.XmlMediaTypeFormatter());
and in WebApiConfig code bellow:
config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");

Why is this connection string invalid?

I read:
This ASP.NET page
whatched through the whole EF part of this pluralsight tutorial
And read nearly every web page ( including SO ones ) containing this error:
Format of the initialization string does not conform to specification starting at index 0
I still do not understand why this connection string is invalid:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-couleur-emotion.mdf;Initial Catalog=aspnet-couleur-emotion;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
My DBContext looks like this:
namespace couleur_emotion.Models
{
public class CouleurEmotionDB : DbContext
{
public CouleurEmotionDB()
: base("name==DefaultConnection")
{
}
public DbSet<PageModel> Pages {get;set;}
}
}
I've tried many different connection strings. Not even once was the database file even created. IT fails at the first call to:
var model = _db.Pages.ToList();
When trying to create the DB. Note the above line is in a class containing:
CouleurEmotionDB _db = new CouleurEmotionDB();

C# change custom connection string at runtime

Bit new on EF. I'm, creating application where user selects database from computer. and now i want to change connection string to match location of the database for example : this is current connection string that points to database location somewhere on disk (C:\Users\student\Documents\TestData.md) :
add name="test" connectionString="metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;attachdbfilename="C:\Users\student\Documents\TestData.mdf";integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
now when user selects new database from disk the connection sting needs to change to location where new database is located (C:\Users\student\Desktop\NewSelectedDatabase.mdf) :
add name="test" connectionString="metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;attachdbfilename="C:\Users\student\Desktop\NewSelectedDatabase.mdf";integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
now i've created filedialog so user can select database and to get its adress . i've also changed my edmax to to recive custom connection string :
public partial class Tester : DbContext
{
public Tester()
: base("name=Test")
{
}
public Tester(string customcs)
: base(customcs)
{
}
now my problem is what do i pass to constructor as custom connection string ? i hope you understood me because i'm realy bad at english and explainig things
When you have the EF designer up, on the properties window is a connectionstring setting. Once you have everything set as you like, clear that setting to none. It rewrites the generated code to accept a connection string passed in on instantiating.
var mything= new dbcontext (connstring)
Another option would be to just create a new class (.cs) file giving it the same namespace that your Tester EF context belongs to, and paste this in there:
public partial class Tester : DbContext {
public Tester(string _connectionString) : base(ConnectionString(_connectionString)) {
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.AutoDetectChangesEnabled = false;
}
private static string ConnectionString(string _connectionString) {
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.ProviderConnectionString = _connectionString;
entityBuilder.Metadata = "res://*/Models.Tester.csdl|res://*/Models.Tester.ssdl|res://*/Models.Tester.msl";
entityBuilder.Provider = "System.Data.SqlClient";
return entityBuilder.ToString();
}
}
Notice it's a partial class (just like the auto-generated ones for Tester are) -- and so you're adding to the auto-generated class made by EF (again, make sure they're in the same namespaces, so it really is an addition to the partial class, not just you off making your own little one).
This way, you're adding a new construction instantiation (that's passing a connection string) that gets modified into the right entity-connection-string builder (via the private static ConnectionString method).
var myThing = new Tester(ConfigurationManager.ConnectionStrings["db_DBName"].ToString());
I have one line in the web.config for the connection:
<add name="db_DBName" connectionString="Data Source=DBSERVER;initial Catalog=DBNAME;" providerName="System.Data.SqlClient" />
the build target defines its transformantion, and I just pass the same string into the code all the time.

Unit test in EF data model class

hi i am generated an CRUD operations in entity frame work in mvc4. Now i Unit test that classes .. i am using the following code in controller for creation
[HttpPost]
public ActionResult Create(Member member)
{
if (ModelState.IsValid)
{
db.Members.Add(member);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(member);
}
and i am using the test code for testing this is,
[TestMethod]
public void Create()
{
MemberController me = new MemberController();
var mem = new Member();
mem.MemID = 123;
mem.MemName = "sruthy";
var result = (RedirectToRouteResult)me.Create(mem);
Assert.AreEqual("Index", result.RouteValues["action"]);
}
i am just try to test the create class. but it shows the following error
Test failed: Create
Message: Test method SmpleTest.MemberTest.Create threw exception:
System.data>ProviderIncomactibleException:An error occured while
getting provider information from the database. This can be cased by
Entity Framework using an incorrect connection string. Check the inner
exception for details and ensure that the connection string is
correct.--->System.data.ProviderIncompatibleException:The provide did
not return a ProviderManifestToken string.--->
System.Data.SqlClient.SqlException:A network- related or intace
specific error occured while establishing a connection to SQL Server.
The server was not found or was not accesable. Varify that the
instance name is correct and the SQL Server is configured to allow
remote connections.(proider:SQL Network Interfaces, error:26-Error
Locating Server/Instance Specified)
This is my connection string
<connectionStrings>
<add name="SampleDataContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Sample;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Sample.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
Normally Create operation is worked with this connection string. Can anybody please help me to identify the problem. Thank you
Please add the connection string in test case project and your return action is Create not the Index.

Categories

Resources