I'm making an extremely simple CMS in ASP.NET MVC 3 (just to learn it) but I've run into complications.
I'm using the EntityFramework (code first), and I'm trying to store articles in MySQL database (articles have standard properties like id, date, headline and content). My Create method is from some tutorial and looks as follows:
[HttpPost]
public ActionResult Create(Article newArticle)
{
if (ModelState.IsValid)
{
db.Articles.Add(newArticle);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(newArticle);
}
}
I have generated a Create view (using razor engine) without changing anything. Everything works fine unless I try to enter longer text into the field for the "content" property (more than 20 or 30 words). Then I get a
System.Data.Entity.Validation.DbEntityValidationException
was unhandled by user code
error at the db.SaveChanges() line. I have tried changing the column I use for storing articles' content to text and then to nvarchar(max) (using Visual Studio's Server Explorer, I hade to change some setting before it let me do that) but it made no difference.
Is there anything obvious I'm missing?
I have pretty much no background in web and database development. Thanks in advance for any hints.
There is probably a mapping for the Content property that limits its size by default.
Try and decorate the Content property of your model with the StringLengthAttribute:
public class Article
{
[StringLength(4000)]
public string Content { get; set; }
}
Is this using EF4.1 RC?
I believe that this might be caused by the default length of varchars, which is set to 128.
ado.net blog post - as stated in the post, you might need to set the maxlength higher .
I believe that this is tested by EF itself, and not at the DB level and that's why your change did not help.
Related
I am programming a way of displaying products that I get from a MySQL database based on user input. My products have a property (size) that can either be represented by a string, by an object of the type Size (another domain class holding three float-values) or be missing alltogether.
Currently my Product-Class has one property for each representation, both of which are nullable. In my view I have one specific place where this property should be displayed.
Now my question is, where do I handle the problem of determining which representation I have for a specific object?
I would be able to include an if-condition in my gsp-template but that seems to be bad practice.
I would be able to have the service that does the query handle the results and build a single size-property to pass to the template but that doesn't seem right either.
Is the problem in my database design?
Do I have to change my domain-model?
I am sorry for the very general question, I can definitely change that once I know where exactly I need to change something. Thanks a lot already!
One way to solve your problem would be to use an additional transient field that would be used in your views, but would not be persisted in your database.
class Product {
String sizeString
Size sizeSize
getSize() { sizeString ?: sizeSize.toString() }
static transients = ['size']
}
In the excellent mvvmcross-library I can use RIO binding to prevent unreadable code:
public INC<String>Title = new NC<String>();
Then I can read and write values using Title.Value. Makes the models much more readable.
Normally, this property would be written as:
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged("Title");
}
}
But when I want to use sqlite-net, these fields cannot be streamed to the database because they are not basic types with a getter and setter.
I can think of a few options how to get around that:
Make a new simple object that is similar to the model, but only with
the direct db-fields. And create a simple import-export static
method on the model. This also could prevent struggling with complex
model-code that never needs to relate to the actual database.
Make sqlite-net understand reading NC-fields. I read into the code of the mapper, but it looks like this is going to be a lot of work because it relies on the getter-setter. I did not find a way to insert custom mapping to a type, that could be generic.
Remove RIO and just put in all the code myself instead of relying on RIO.
Maybe someone has some advice?
Thanks Stuart. It was exactly my thought, so I did implement it that way: my (DB) Models do not contain RIO. Only my viewmodels do, and they reference a Model that is DB-compatible.
So, for posterity the following tips:
- Do not use RIO in your models that need to be database-backed.
- Reference models in your viewmodels. In the binding you can use the . (dot) to reference this model.
This keeps them nicely separated. This gives you also another advantage: if you need to reuse a model (because the same object might be displayed twice on the screen), but under different circumstances, it is much easier to handle this situaties to find this already instantiated model.
I am making ajax calls to my webservice (using MS ajax framework - Telerik comps uses it actually). I am returning one of the Entity classes generated by the dbml. It used to work fine, but when I added the associations it started throwing an exception on the server, saying "a circular reference was detecting when serializing type "
I worked around it for now, but I'd really like to know what is happening. Thanks
This is because the relation is mapped with navigation properties both ways. ie you can use:
myCustomer.Orders
but also
order.Customer
You could try marking one of them non-public in the dbml, then if you need a public property, create it in the partial class, so you can mark the property with XmlIgnoreAttribute:
partial class Order
{
[XmlIgnore]
public Customer Customer
{
get { return InternalCustomer; }
set { InternalCustomer = value; }
}
}
I am trying to see if there is a way to include "descriptive text" in my junit reports by way of javadocs. JUnit 4 doesnt seem to support the 'description' attribute for the #Test annotation like TestNG does.
So far from what I have researched there is only one tool out there called javadoc-junit (http://javadoc-junit.sourceforge.net/). However I could not get this to work since it seems to be incompatible with Junit 4.
What I want is some way to provide a sentence or two of text with my each test method in the JUnit report. JavaDoc is no good since the target audience will have to swtich between JavaDoc and the Junit Report to see documentation and/or test stats.
Anyone know of anything else I could use with minimal effort?
Best,
Ray J
In JUnit 5 there is a way to annotate every test with a #DisplayName. The declared test classes can have text, special characters and emojis.
The declared text on each test is visible by test runners and test reports.
The Javadoc says:
public #interface DisplayName
#DisplayName is used to declare a custom display name for the annotated test class or test method.
Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.
And the User Guide:
import org.junit.gen5.api.DisplayName;
import org.junit.gen5.api.Test;
#DisplayName("A special test case")
class DisplayNameDemo {
#Test
#DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}
#Test
#DisplayName("╯°□°)╯")
void testWithDisplayNameContainingSpecialCharacters() {
}
#Test
#DisplayName("😱")
void testWithDisplayNameContainingEmoji() {
}
}
There's also rather recent solution called Allure. That's a Java-based test execution report mainly based on adding supplementary annotations to the code. Existing annotations include:
custom description: #Description("A cool test")
grouping by features or stories: #Features({"feature1", "feature2"}), #Stories({"story1", "story2" })
marking methods executed inside test case as steps: #Step (works even for private methods)
attachments: #Attachment(name = "Page screenshot", type = "image/png")
See their wiki and example project for more details.
I don't put javadocs in JUnit tests. I usually make the name of the method descriptive enough so it's as good as or better than any comment I could come up with.
I could imagine, that the Framework for Integrated Tests (FIT) would be a nice and clean solution.
What does FIT do?
FIT is a framework that allows to write tests via a table in a Word document, a wiki table or an html table.
Every character outside of a table is ignored by FIT and let you enter documentation, description, requirements and so on.
How does on of these tables look like?
Imagine a function MyMath.square(int) that squares it's input parameter. You have to build a so called Fixture, being an adapter between your MyMath and the following table:
class.with.Fixture.Square
x square()
2 4
5 25
The first column describes input values, the second the expected result. If it's not equal, this field is marked as red.
How does a Fixture look like?
For the given example, this would be the correct fixture:
package class.with.Fixture // Must be the same as in the fist row of the table
public class Square extends Fixture {
public int x; // Must be the same as in the second row
public int square() { // Must be the same as in the second row
return MyMath.square(x);
}
}
Probably, you can use FIT for your requirements.
Feel free to comment my answer or edit your question for more information!
I am using C# and trying to read a CSV by using this connection string;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\rajesh.yadava\Desktop\orcad;Extended Properties="Text;HDR=YES;IMEX=1;FMT=Delimited"
This works for tab delimited data.
I want a connection string which should for tab delimited as well as comma(,) and pipe(|).
How can I make a generic connection string for CSV.
Thanks
Rajesh
Is the filehelpers library an option?
I know this doesn't answer your questions, but here's a word of warning.
I've had to create my own reader as you don't get the correct drivers if you ever run on a 64 bit system.
If your software will ever run on a 64 bit system, make sure you test it first and that the oledb or odbc drivers will be present.
In case that you need a fast sequential access to the CSV file, the Fast CSV Reader could be an option. I have used it on a project some time ago with great success. It is supposed to be optimized quite well and also provides a cached version, if you need it. Additionally, it was updated several times since it was first released back in 2005 (last update in 2008-10-09) and it supports basic databinding by implementing System.Data.IDataReader.
Here's a few links from the net discussing this issue:
Manipulating CSV Files
ConnectionStrings.com
How To Open Delimited Text Files Using the Jet Provider's Text IIsam
Is using the TextFieldParser class an option?
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx
Without rolling a custom solution, I'm not sure there's a straightforward way to support more than one delimiter. This page suggests that through schema.ini you can choose between:
TabDelimited
CSVDelimited
one specific character (except double quote)
fixed width
class CSVFile extends SplFileObject
{
private $keys;
public function __construct($file)
{
parent::__construct($file);
$this->setFlags(SplFileObject::READ_CSV);
}
public function rewind()
{
parent::rewind();
$this->keys = parent::current();
parent::next();
}
public function current()
{
return array_combine($this->keys, parent::current());
}
public function getKeys()
{
return $this->keys;
}
}
then use with:
$csv = new CSVFile('exmaple.csv');
and you can iterate through lines using:
foreach ($csv as $line)
{